Javascript 将 JSON 数据传递给 jQuery 中的 .getJSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8232197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing JSON data to .getJSON in jQuery?
提问by TruMan1
I am trying to pass a JSON object to .getJSON but I keep getting a bad request error. This is what I am trying:
我正在尝试将 JSON 对象传递给 .getJSON,但我不断收到错误的请求错误。这就是我正在尝试的:
var data = {
"SomeID": "18",
"Utc": null,
"Flags": "324"
};
$.getJSON("https://somewhere.com/AllGet?callback=?", JSON.stringify(data), function (result) {
alert(result);
});
Currently to get it working, I have to do this, but I do not like how I have to manually construct the query string:
目前为了让它工作,我必须这样做,但我不喜欢我必须手动构造查询字符串的方式:
$.getJSON("https://somewhere.com/AllGet?SomeID=18&Utc=&Flags=324&callback=?", null, function (result) {
alert(result);
});
Anyone know how to make requests easier with JSON objects being passed in? I would appreciate any help or advise.
任何人都知道如何通过传入的 JSON 对象使请求更容易?我将不胜感激任何帮助或建议。
回答by craniumonempty
according to the site, this is valid:
根据该网站,这是有效的:
$.getJSON("test.js", { name: "John", time: "2pm" }, function(json) {
alert("JSON Data: " + json.users[3].name);
});
so try:
所以尝试:
var data = {
SomeID: "18",
Utc: null,
Flags: "324"
};
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
回答by Senad Me?kin
Dont use JSON.stringfy, just pass data as it is.
不要使用 JSON.stringfy,只需按原样传递数据。
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
回答by Jacob
When you provide data to a jQuery GET request, it expects an object, not a JSON string, for constructing the query string parameters. Try changing your original code to just this:
当您向 jQuery GET 请求提供数据时,它需要一个object而不是 JSON 字符串来构造查询字符串参数。尝试将您的原始代码更改为:
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
回答by Johan
why exactly do you need a callback? (Ow wait, jsonp) I'd try the following first:
为什么你需要回调?(等等,jsonp)我会先尝试以下操作:
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function(result) {
alert(result);
});
somewhere in firebug and see if it returns what you expect. I'm not sure what a string as data does, but just giving an object works fine afaik.
在萤火虫的某个地方,看看它是否返回您期望的内容。我不确定字符串作为数据的作用,但只提供一个对象就可以正常工作afaik。
回答by Abdul Munim
You don't need to do JSON.stringfy
, just pass the JSONobject, jQuery will construct your URL parameter with that
您不需要做JSON.stringfy
,只需传递JSON对象,jQuery 将使用该对象构造您的 URL 参数
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
回答by suketup
I tried encoding the json and it worked.
我尝试对 json 进行编码并且它起作用了。
Not sure how efficient or practical it is, sharing it just as a work around for above question.
不确定它的效率或实用性如何,将其分享为上述问题的解决方法。
$.getJSON("https://somewhere.com/AllGet?data="+encodeURI(JSON.stringify(data)), function (result) {
alert(result);
});
回答by Rafay
$.getJSON("https://somewhere.com/AllGet?callback=?", {SomeID:"18",Utc:null,Flags:"324"}, function (result) {
alert(result);
});
OR
或者
var data = {
"SomeID": "18",
"Utc": null,
"Flags": "324"
};
$.getJSON("https://somewhere.com/AllGet?callback=?",
{
SomeID:data.SomeID,
Utc:data.Utc,
Flags:data.Flags
},
function (result) {
alert(result);
});