Passing JSON data to .getJSON in jQuery?

旧街凉风 提交于 2019-11-30 13:14:50

问题


I am trying to pass a JSON object to .getJSON but I keep getting a bad request error. This is what I am trying:

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.


回答1:


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/




回答2:


Dont use JSON.stringfy, just pass data as it is.

$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
    alert(result);
});



回答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:

$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
    alert(result);
});



回答4:


You don't need to do JSON.stringfy, just pass the JSON object, jQuery will construct your URL parameter with that

$.getJSON("https://somewhere.com/AllGet?callback=?", data, function (result) {
    alert(result);
});



回答5:


why exactly do you need a callback? (Ow wait, jsonp) I'd try the following first:

$.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.




回答6:


$.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:


I tried encoding the json and it worked.

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);
 });


来源:https://stackoverflow.com/questions/8232197/passing-json-data-to-getjson-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!