简体   繁体   English

JSON无法使用.getJSON进行解析

[英]JSON not parsing using .getJSON

having an issue with simple JSON parsing, wondering if someone could quickly spot any errors in this syntax? 对简单的JSON解析有疑问,想知道是否有人可以快速发现此语法中的任何错误?

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

the JSON file is as follows: JSON文件如下:

{
 "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. 由于某种原因,它不喜欢“ data.foo”位。 I can use static data, but it's definitely not reading / parsing the JSON data. 我可以使用静态数据,但绝对不能读取/解析JSON数据。 Not sure if it has to do with the URL I am using? 不确定是否与我使用的URL有关? (The file has been validated using JSONLint) (该文件已使用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. 现在运行MAMP,它可以像上面那样工作。

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. 如果您有Firebug,请尝试逐步执行代码来调试代码。 If you don't add a few alert("this code was run"); 如果您不添加一些alert("this code was run"); in different places (before the ajax, after the ajax and in the callback function). 在不同的位置(在ajax之前,在ajax之后和在回调函数中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM