4

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.

Nitesh singh
  • 915
  • 11
  • 21

1 Answers1

5

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • i use your code But at this time i could not get any data from json. – Nitesh singh Sep 24 '15 at 17:05
  • @niteshsingh: Then there's *another* problem. You'll need to use the tools built into your browser to find out what it is. Use Ctrl+Shift+I (that's an "eye") or F12 (depends on the browser and OS) or look through the menus. Look in the "web console" for errors, etc. – T.J. Crowder Sep 24 '15 at 17:09
  • @niteshsingh: I've added a note to the end of the answer, though, which may be what's going on now you've corrected the async thing. – T.J. Crowder Sep 24 '15 at 17:11
  • In console i am not get any error ,but i will check for localhost issue . – Nitesh singh Sep 24 '15 at 17:12