开发者

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 reas开发者_JAVA技巧on 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.


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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜