getJSON vs. optJSON
A value in the JSON may be optional, so using optJSONObject is better because you just have to check if it is null or not and continue your function.
A value in the JSON may be optional, so using optJSONObject is better because you just have to check if it is null or not and continue your function.
Assuming you mean “file on a local filesystem” when you say .json file. You’ll need to save the json data formatted as jsonp, and use a file:// url to access it. Your HTML will look like this: <script src=”https://stackoverflow.com/questions/6711002/file://c:\data\activity.jsonp”></script> <script type=”text/javascript”> function updateMe(){ var x = 0; var activity=jsonstr; foreach (i in activity) { date … Read more
getJSON() is simply shorthand for the ajax() function with the dataType:’json’ set. The ajax() function will let you customize a lot about the request. $.ajax({ url: “https://stackoverflow.com/questions/4116992/MyArray.json”, async: false, dataType: ‘json’, success: function (response) { // do stuff with response. } }); You still use a callback with async:false but it fires before it execution … Read more
Since $.getJSON() uses ajax configurations, just set the global ajax configs: // Set the global configs to synchronous $.ajaxSetup({ async: false }); // Your $.getJSON() request is now synchronous… // Set the global configs back to asynchronous $.ajaxSetup({ async: true });
This will do it: var json = (function () { var json = null; $.ajax({ ‘async’: false, ‘global’: false, ‘url’: my_url, ‘dataType’: “json”, ‘success’: function (data) { json = data; } }); return json; })(); The main issue being that $.getJSON will run asynchronously, thus your Javascript will progress past the expression which invokes it … Read more
Since $.getJSON is async, I think your console.log(list.length); code is firing before your array has been populated. To correct this put your console.log statement inside your callback: var list = new Array(); $.getJSON(“json.js”, function(data) { $.each(data, function(i, item) { console.log(item.text); list.push(item.text); }); console.log(list.length); });
I agree with sunetos that you’ll have to use the $.ajax function in order to pass request headers. In order to do that, you’ll have to write a function for the beforeSend event handler, which is one of the $.ajax() options. Here’s a quick sample on how to do that: <html> <head> <script src=”http://code.jquery.com/jquery-1.4.2.min.js”></script> <script … Read more
Some modern browsers have support for parsing JSON into a native object: var var1 = ‘{“cols”: [{“i” ……. 66}]}’; var result = JSON.parse(var1); For the browsers that don’t support it, you can download json2.js from json.org for safe parsing of a JSON object. The script will check for native JSON support and if it doesn’t … Read more
Your code just needs a trigger for it to be enabled. This will allow you to disable cache in all future ajax $(document).ready(function() { $.ajaxSetup({ cache: false }); });
This is how it worked for me… $.ajaxSetup({ cache: false }); $.getJSON(“/MyQueryUrl”,function(data,item) { // do stuff with callback data $.ajaxSetup({ cache: true }); });