i user the code:
var test = $.getJSON( "about.json");
console.log(JSON.stringify(test));
and i about.json is on same file directory but i only got {"readyState":1} i don't know what is the issue.please help me if i am wrong.
i user the code:
var test = $.getJSON( "about.json");
console.log(JSON.stringify(test));
and i about.json is on same file directory but i only got {"readyState":1} i don't know what is the issue.please help me if i am wrong.
getJSON
doesn't return what it gets, because it can't; the operation is asynchronous. Instead, it lets you provide a callback:
$.getJSON("about.json", function(test) {
console.log(JSON.stringify(test));
});
jQuery will call your function later, when the data has come back.
The reason you're seeing what you're seeing is that you're outputting a JSON string for the jqXHR
object that $.getJSON
returns. Apparently, the only enumerable, non-function, non-undefined
property on that object is readyState
, so that's all you see.
and i about.json is on same file directory
(and from your comment below after changing to using the code above)
But at this time i could not get any data from json
Note that if you're doing this in an HTML file you've opened locally (e.g., a file:
URL, not an http:
or https:
URL), some browsers (such as Chrome) disallow all ajax calls. When doing web development, it's important to use a server (even if it's localhost
), because a lot of things either don't work or work differently when you're using local files.