json 未捕获语法错误:意外标记: [英] json Uncaught SyntaxError: Unexpected token :

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

问题描述

尝试拨打电话并检索一个非常简单的单行 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

这是原始请求:

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

这是原始回复:

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 : colours.json:1

The JSON is coming back in the response (red : #f00), but Chrome reports Uncaught SyntaxError: Unexpected token : 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?

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

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

EDIT 2 - 这个 URL 工作正常 'http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json' 我注意到它返回为TYPE: text/javascript 和 Chrome 没有抛出 Unexpected Token.我已经测试了其他几个 url,唯一一个不抛出 Unexptected Token 的是 wunderground,它以 TYPE: text/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.

作为 text/plain 和 application/json 返回的流没有被正确解析.

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

推荐答案

您已经告诉 jQuery 期待 JSONP 响应,这就是 jQuery 将 callback=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 修复为 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 未捕获语法错误:意外标记:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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