views:

76

answers:

2

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.

A: 

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).

Marius
good call, it's definitely tripping up on the data.foo (I am trying 'before ajax' / 'after ajax' alerts and it's not returning the 'after ajax' one.GET request returns a 200OK though, hmmm
gleddy
remember that ajax is asynchronous, so if an alert right after `$.getJSON();` isn't being called, then you have a syntax error in the ajax code. The alert should be called right after the first one, and before the one in the callback function.
Marius
+1  A: 

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!

gleddy
That is correct, thus my original question. Any Ajax request made (like this) must be on the same domain.
bdl