Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

javascript - Passing JSON data to .getJSON in jQuery?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...