JSON未捕获的SyntaxError:意外的标记: [英] json Uncaught SyntaxError: Unexpected token :

查看:918
本文介绍了JSON未捕获的SyntaxError:意外的标记:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图拨打电话和检索一个非常简单的,一条线,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

这里的RAW响应:

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"}

该JSON是回来的响应(红色:#F00),而Chrome报道的未捕获的SyntaxError:意外的标记:colors.json:1

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

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

如果我贴colors.json到JSLint的内容,JSON的验证。

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?

修改 - 在jQuery.ajax()调用上面运行完美的jsfiddle.net,并返回警报成功的预期。

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

编辑2 - 这个URL工作正常http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json我注意到,它返回类型:文本/ javascript和Chrome浏览器并没有抛出意外的标记。我测试过其他几个网址,唯一一个不抛出Unexptected令牌是返回类型wunderground:文/ JavaScript的。

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.

流返回文本/纯和应用/ JSON没有被正确解析

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

推荐答案

您已经告诉了jQuery期待 JSONP 对此,这也是为什么jQuery的加入了回调= jQuery16406345664265099913_1319854793396和放大器; _ = 1319854793399 部分的URL(你可以看到在您的要求转储)

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

你在返回的JSON,不是JSONP。您的回应看起来像

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

{"red" : "#f00"}

和jQuery的期待是这样的:

and jQuery is expecting something like this:

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

如果你确实需要使用JSONP来绕过同源策略,再服 colors.json 服务器需要能够真正返回一个JSONP响应。

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.

如果同源策略是不是一个问题,您的应用程序,那么你只需要修复您的 jQuery.ajax <在的dataType / code>呼吁为 JSON 而不是 JSONP

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.

这篇关于JSON未捕获的SyntaxError:意外的标记:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆