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.
Projects In JavaScript & JQuery To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.
The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.
jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With