gleddy
gleddy

Reputation: 425

JSON not parsing using .getJSON

having an issue with simple JSON parsing, wondering if someone could quickly spot any errors in this syntax?

function getFavs() {
    $.getJSON('http://www.example.com/scripts/test.json', function(data) {
        $('#main-content').html(data.foo);  
    });
}

the JSON file is as follows:

{
 "foo": "The quick brown fox jumps over the lazy dog.",
 "bar": "ABCDEFG",
 "baz": [
     52,
     97
   ]
}

For some reason it doesn't like the 'data.foo' bit. I can use static data, but it's definitely not reading / parsing the JSON data. Not sure if it has to do with the URL I am using? (The file has been validated using JSONLint)

thanks for any clues.

Upvotes: 0

Views: 265

Answers (2)

gleddy
gleddy

Reputation: 425

this is solved, seemed to be running locally and calling to a live server would be cross-domain I figure. Running MAMP now and it works as should above.

thanks!

Upvotes: 1

Marius
Marius

Reputation: 59009

Things look ok, but check that the function is actually called. If you have Firebug, try to debug the code by running through it step by step. If you don't add a few alert("this code was run"); in different places (before the ajax, after the ajax and in the callback function).

Upvotes: 0

Related Questions