$ .getJSON因解析错误而失败

时间:2010-07-29 19:20:03

标签: jquery getjson

我正在尝试使用jQuery.getJSON从我自己不拥有的域中获取数据。我过去一直在努力,但这次它一直都失败了。我在浏览器中收到了回复,但Chrome显示SyntaxError: Unexpected token :,Firefox说invalid label

这有一些特点。假设代码是MY_JSON_RESPONSE(不是变量,只是“别名”)。这些都返回预期的对象:

a = MY_JSON_RESPONSE;
eval(MY_JSON_RESPONSE);

这是我提出的要求:

$.getJSON("http://xxx.yyy.zzz/abc?callback=?",
   function(data) {
     console.log(data);
   });

编辑:以下是匿名回复数据:

{"aaaaaaa":"aaaaaaaa aaaaa aaa aaaaaaaaaa","aaaaa":"aaaaaaaa aaaaa aaa","aa":"0000000","aaaa_aaaaaaa":"aaa aaaaa","aaaaaaa_aaaaaaa":"aa aaa aaaaaaaa aa aaaaaaaa aaa aaaaaaa aaaaaa aaa aaa aaaaaaaa aa aaaaaaaa aa aaaaaa aaaaa aaa aaaaa. aaa aaaaaaaa aaaaa aaa aaaaaaaaaa aaaaaaaa aaaaa aaaaa aaaaaaaa aaaaaaaaaaa aaaaaaaaaa aa aaaa 00 aaaaaaa aaaaaaaaa aa aaa aaaaaaaa aaaaa aaa, aa aaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaa, aaaaaaaaa aaa aaaaaaa aaaaaaaa. 00% aa aaaaaaa aaaaaa aaaaa aaaaa aaaaaaaaaaaaa aaaaa aaaaaaaaaaaaa.","aaaaaaaaa":[{"aaaaaa":"aaaaa aaa aaaaaa  aaaaaaaa","aaaaaaa":"aaaaaaaa aa aaaaaa aaa aaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaa","aaaaaaa":"aaaaaaaa aaaa aa &aaaa;aaaaa aaa aaaaaaaaaa aaaaaaaaa&aaaa; - aaa aaa aa, aaaaa aaa a aaaa aaaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a000.00"},{"aaaaaa":"aaaaa","aaaaaaa":"aaaa aaaa aaaa!!!!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaa\/aaa","aaaaaaa":"aaaaaaa aaaaa aaaa aaa aaaa aaaaa aaa aaaaa aaa aaaaaaa aaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aa (aaaaaaa &aaa; aaaaaaaaa)","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a0000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa  aaaaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaaaaa aaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaa","aaaaaaa":"aaaa aaaaaaa aaaaaaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaaaaa","aaaaaaa":"aaaaa aaaaaa�a aaa, aaa! aaaa aaaaaaaa aa aaa aaa! :-) --aaa","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaa - aaaaaaaa aaaaaa aaa","aaaaaaa":"aaaaaa aa aaa aaa aaaaaaaa aaaaa aaaaaa aa aaaa aa aaa aaaaa aaa aaaaa. aaaa aaaa aaaaaa aaaaaaaaa !!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaa aaaaaaaaa","aaaaaaa":"aa aaaaaa aaaa aaaaaaaa aa aaaa aa aaaa aa a aaaa aaaaa aaa aaaaaaaaaa. aaaa aa aa aaaaaa aa a aaaa aaaaa.","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaaaa aaa aaaaaaaaaaa aaaaaaa aaaaaa","aaaaaaa":"aaaaa aaaaa. aaaa aa aaa aaaaaa aaaaaaaaa!!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa  aaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaaaaaaaa aaaaaa","aaaaaaa":"aaaaaaa aaa aaaa aaaaaa�a aaaaaaaaa aaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a0000.00","aaaa_aaa":"\u00a000.00"},{"aaaaaa":"aaaaaaa aaaaaa","aaaaaaa":"aaaaaaaaa aaaaa, aaa! aaaaaaa &aaa; aaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaaaa aaaaaaaa","aaaaaaa":"aaaaaaaa aaaaaaa - aaaaaaa aaaaaaaaa a aaa aaa aaaaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaa","aaaaaaa":"aaaaaaaaaaaa - aaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a0000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaaaaaaa","aaaaaaa":"aaaa aaaa!!!","aaaa":"00\/00\/00","aaaaaa":"\u00a00.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaaa","aaaaaaa":"aaaa aaaa aaaaa, aaaaaaa, aaaaaaa, aaaaa, aaa, aaaa, aaa, aaaaaaaa, aaaa, aaa, aaaaa, aaa, aaa, aaaaa, aaa, aaaa, aaaa, aaaa, aaaaa, aaa, aaaaaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaaa aaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaa","aaaaaaa":"aaaa aaa aa aa aaaa aa a aaaaaaa aa aaaaa! aaa aaa aaaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaa","aaaaaaa":"aa aa aa! :)","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaaa","aaaaaaa":"aaaa a aaaaaa aaaaaaaaa aaaa aa aa aaa :)","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaa-aaaaaaa aaaaaaaa","aaaaaaa":"aaaa aaaaaaaa aa aa aaa aaaa aa aa aaaaaaaaaaa, aaaa a. aaaaaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaa","aaaaaaa":"a aaaaaaaa aa aaa aaaaaaaa aaaaa aaa aa aaaaa aa aaaaa aaa aaaaa aaaaa  aa aaa aaaaaa aa aaaaaaaaa.","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaaaaa","aaaaaaa":"aaaaa aaaaaaaa aaa aaaa aaaaaaa aaaa aaaaaa aaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaa aaaa","aaaaaaa":"aaaa aaaa aaaa aaaa aaaaaaaaaaa aaaa aaa aaa aaaaaaaaa aaaa aaa aa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"}],"aaaa_aaaaaaa":"","aaaa_aaa":"\u00a0000.00","aaaaaaaaa_aaaaaa":"\u00a00,000.00","aaaaaaaaa_aaaaa":"\u00a00,000.00","aaaaaaa_aaaa":"aaaa:\/\/aaa.aaaaaaaaaa.aaa\/\/aaaaa\/aaaaaaa.aaaa?aaaa=aaaaaaa&aaaaaaaaa=aaaaaaaaaaa&aaaaa=000&aaaaaa=000&aaa=aaaa_aaaa0.aaa","aaaaaaaaa_aaaaaa":"\u00a00,000.00","aaaaaaaaa_aaaaaaa":"\u00a00.00","aaa_aaaaaa":true,"aaaaaaaaaa_aaaaaaaa":00}

我想避免在我自己的域上的服务器上执行此操作,以最大限度地减少服务器负载。

4 个答案:

答案 0 :(得分:1)

jQuery 1.4中的JSON解析比以前的版本更严格。根据您的浏览器,它可能使用本机JSON解析,这与eval不同。一个很大的区别是,现在,所有键必须用双引号括起来,而不是单引号。 eval会像你说的那样没有错误,因为JS对引用属性名称并不严格,但它将无法解析JSON。

在将项目转换为jq 1.4时,我自己遇到了一个问题,因为我有一些旧代码手动构建json字符串。例如

return "{'a':12}";  // fails parsing due to single quotes

使用内置的json库要好得多:

return '{"a":12}'; // works
return json_encode(array('a'=>12)); // preferred!
return json.dumps({'a':12}) # etc.

因为他们总是生成有效的json。

答案 1 :(得分:0)

JSON示例无效。它包括这个子字符串:

}, {

也就是说,它结束一个对象并启动另一个对象。所以它似乎是一个对象数组,除了没有括起方括号。

编辑:我的错误!事实上它确实有一个嵌套数组。也许你应该在生成它时“漂亮地打印”它,这样就更容易看出问题出在哪里了?

答案 2 :(得分:0)

有趣的是,根据http://jsonformatter.curiousconcept.com/,JSON是有效的。

正如其他人已经注意到的那样...... jQuery 1.4不会接受格式错误的JSON,但早期版本会(如果你想为某些疯狂的原因指明方向)。

您如何在代码中调用实际的JSON数据元素?我遇到过几次问题......完全有效的JSON,但在jQuery中不容易调用。

一个例子:tumblr(博客引擎)API ...出于某种原因,他们使用了......“ - ”...所以对于博客文章的“正文”他们有“常规身体” “...... jQuery显然非常讨厌这种激情。

你认为你可以......

json.posts[2].regular-body

但那会出错,所以你需要[] ......

json.posts[2]['regular-body']

所以......

如果您可以提供有关如何调用JSON数据元素的更多详细信息,那么它可能会有所帮助吗?

答案 3 :(得分:0)

我现在也遇到同样的问题,我的服务器用正确的json数据进行响应。我遵循您使用的相同代码模式。我在这里评论之前做过R& D来解决这个错误。正如我们使用" callback =?"在url中,它倾向于将对象请求为JSONP。它假设响应来自JSONP,但响应以纯JSON格式出现。一旦响应得到它试图根据JSONP解析而不是像JSON。如果我错了请搞定,如果您的问题得到解决,也请告诉我。感谢。