json未捕获语法错误:意外的令牌:(json Uncaught SyntaxError: Unexpected token :)

编程入门 行业动态 更新时间:2024-10-23 21:35:35
json未捕获语法错误:意外的令牌:(json Uncaught SyntaxError: Unexpected token :)

尝试拨打一个非常简单的一行JSON文件。

$(document).ready(function() { jQuery.ajax({ type: 'GET', url: 'http://wncrunners.com/admin/colors.json' , dataType: 'jsonp', success: function(data) { alert('success'); } }); });//end document.ready

这是RAW请求:

GET http://wncrunners.com/admin/colors.json?callback=jQuery16406345664265099913_1319854793396&_=1319854793399 HTTP/1.1 Host: wncrunners.com Connection: keep-alive Cache-Control: max-age=0 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2 Accept: */* Referer: http://localhost:8888/jquery/Test.html Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

这是RAW响应:

HTTP/1.1 200 OK Date: Sat, 29 Oct 2011 02:21:24 GMT Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d SE/0.5.3 Last-Modified: Fri, 28 Oct 2011 17:48:47 GMT ETag: "166a2402-10-4eaaeaff" Accept-Ranges: bytes Content-Length: 16 Content-Type: text/plain Connection: close {"red" : "#f00"}

JSON正在回复(红色:#f00),但Chrome报告Uncaught SyntaxError:Unexpected token:colors.json:1

如果我直接导​​航到url本身,则返回JSON并显示在浏览器中。

如果我将colors.json的内容粘贴到JSLINT中,则json验证。

任何想法为什么我不能得到这个错误,我从来没有让它成功回调?

编辑 - 上面的jQuery.ajax()调用在jsfiddle.net运行完美,并按预期的方式返回警报“成功”。

编辑2 - 这个URL工作正常'http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json'我注意到它返回为TYPE:text / javascript和Chrome没有抛出意外令牌 我已经测试了几个其他的url,唯一一个不会抛出未认可的令牌是返回为TYPE:text / javascript的wunderground。

以text / plain和application / json返回的流未正确解析。

Trying to make a call and retrieve a very simple, one line, JSON file.

$(document).ready(function() { jQuery.ajax({ type: 'GET', url: 'http://wncrunners.com/admin/colors.json' , dataType: 'jsonp', success: function(data) { alert('success'); } }); });//end document.ready

Here's the RAW Request:

GET http://wncrunners.com/admin/colors.json?callback=jQuery16406345664265099913_1319854793396&_=1319854793399 HTTP/1.1 Host: wncrunners.com Connection: keep-alive Cache-Control: max-age=0 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2 Accept: */* Referer: http://localhost:8888/jquery/Test.html Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Here's the RAW Response:

HTTP/1.1 200 OK Date: Sat, 29 Oct 2011 02:21:24 GMT Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d SE/0.5.3 Last-Modified: Fri, 28 Oct 2011 17:48:47 GMT ETag: "166a2402-10-4eaaeaff" Accept-Ranges: bytes Content-Length: 16 Content-Type: text/plain Connection: close {"red" : "#f00"}

The JSON is coming back in the response (red : #f00), but Chrome reports Uncaught SyntaxError: Unexpected token : colors.json:1

If I navigate directly to url itself, the JSON is returned and is displayed in the browser.

If I paste the contents of colors.json into JSLINT, the json validates.

Any ideas why I can't get this error and I never make it to the success callback?

EDIT - the jQuery.ajax() call above runs perfect at jsfiddle.net, and returns the alert 'success' as expected.

EDIT 2 - this URL works fine 'http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json' I noticed that it returned as TYPE: text/javascript and Chrome did not throw the Unexpected Token. I've tested several other url's and the ONLY one that does not throw the Unexptected Token is the wunderground that is returned as TYPE: text/javascript.

Streams returned as text/plain and application/json are not being parsed correctly.

最满意答案

你已经告诉jQuery想要一个JSONP响应,这就是为什么jQuery已经添加了一个callback=jQuery16406345664265099913_1319854793396&_=1319854793399的URL(你可以在转储请求中看到这一点) callback=jQuery16406345664265099913_1319854793396&_=1319854793399 。

你所返回的是JSON,而不是JSONP。 你的回应看起来像

{"red" : "#f00"}

而jQuery正在等待这样的事情:

jQuery16406345664265099913_1319854793396({"red" : "#f00"})

如果您实际需要使用JSONP来绕过相同的原始策略,则服务于colors.json的服务器需要能够实际返回JSONP响应。

如果相同的起始策略不是您的应用程序的问题,那么您只需要将jQuery.ajax调用中的dataType修复为json而不是jsonp 。

You've told jQuery to expect a JSONP response, which is why jQuery has added the callback=jQuery16406345664265099913_1319854793396&_=1319854793399 part to the URL (you can see this in your dump of the request).

What you're returning is JSON, not JSONP. Your response looks like

{"red" : "#f00"}

and jQuery is expecting something like this:

jQuery16406345664265099913_1319854793396({"red" : "#f00"})

If you actually need to use JSONP to get around the same origin policy, then the server serving colors.json needs to be able to actually return a JSONP response.

If the same origin policy isn't an issue for your application, then you just need to fix the dataType in your jQuery.ajax call to be json instead of jsonp.

更多推荐

本文发布于:2023-07-29 20:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   意外   语法错误   json   token

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!