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对象简化请求吗?我希望得到任何帮助或建议。
7 个解决方案
#1
20
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);
});
edit: http://api.jquery.com/jQuery.getJSON/
编辑:http://api.jquery.com/jQuery.getJSON/
#2
3
Dont use JSON.stringfy, just pass data as it is.
不要使用JSON。stringfy只传递数据。
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
#3
3
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请求提供数据时,它需要一个对象(而不是JSON字符串)来构造查询字符串参数。尝试将您的原始代码修改为以下内容:
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
#4
1
You don't need to do JSON.stringfy
, just pass the JSON object, jQuery will construct your URL parameter with that
不需要使用JSON。stringfy只传递JSON对象,jQuery将用它构造URL参数
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
#5
1
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.
在firebug的某个地方,看看它是否能返回您所期望的。我不确定字符串作为数据的功能是什么,但是仅仅给出一个对象就可以很好地工作。
#6
0
$.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);
});
#7
0
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);
});
#1
20
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);
});
edit: http://api.jquery.com/jQuery.getJSON/
编辑:http://api.jquery.com/jQuery.getJSON/
#2
3
Dont use JSON.stringfy, just pass data as it is.
不要使用JSON。stringfy只传递数据。
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
#3
3
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请求提供数据时,它需要一个对象(而不是JSON字符串)来构造查询字符串参数。尝试将您的原始代码修改为以下内容:
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
#4
1
You don't need to do JSON.stringfy
, just pass the JSON object, jQuery will construct your URL parameter with that
不需要使用JSON。stringfy只传递JSON对象,jQuery将用它构造URL参数
$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
alert(result);
});
#5
1
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.
在firebug的某个地方,看看它是否能返回您所期望的。我不确定字符串作为数据的功能是什么,但是仅仅给出一个对象就可以很好地工作。
#6
0
$.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);
});
#7
0
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);
});