user1446495
user1446495

Reputation: 13

JSON data not loading with $.getJSON

I'm trying to load JSON data into jQuery using $.getJSON. However, the alert I call does not show up (though I know the js is linked properly because other alerts outside the $.getJSON show up). I also know the "test.json" file is in the correct directory.

I've boiled it down to the simplest possible code to best find the answer to what I'm doing wrong:

The getjson.js file (inside a document ready):

$.getJSON("test.json", function(json) {
    alert("JSON Data: " + json.statename);
});

The JSON data in test.json is simply:

 {"statename": "Hawaii"}

And yet no alert shows up when the getjson.js file is loaded! In Firebug I see no request being sent either. I've Google'd around about this, but it looks to me like my syntax is ok, and I'm a newbie at JSON and jQuery so I just can't see what I'm doing wrong here.

Thanks a lot for any input here, going completely crazy with this!

Upvotes: 1

Views: 1346

Answers (1)

WojtekT
WojtekT

Reputation: 4775

You have to provide correct url to .getJSON call. It won't work for local files on your computer.

Upvotes: 1

Related Questions