【发布时间】:2011-12-17 16:58:39
【问题描述】:
尝试调用并检索一个非常简单的一行 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
这是原始请求:
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 : colors.json:1
如果我直接导航到 url 本身,则会返回 JSON 并显示在浏览器中。
如果我将 colors.json 的内容粘贴到 JSLINT 中,则 json 会验证。
任何想法为什么我不能得到这个错误并且我从来没有成功回调?
EDIT - 上面的 jQuery.ajax() 调用在 jsfiddle.net 上完美运行,并按预期返回警报“成功”。
EDIT 2 - 这个 URL 工作正常 'http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json' 我注意到它返回为TYPE: text/javascript 和 Chrome 没有抛出 Unexpected Token。我已经测试了其他几个 url,唯一没有抛出 Unexpected Token 的是 wunderground,它以 TYPE: text/javascript 返回。
以 text/plain 和 application/json 返回的流未正确解析。
【问题讨论】:
-
附带说明,我意识到在浏览器中点击 url 时,“#”实际上并没有出现在 json 中。 wncrunners.com/admin/colors.json
-
感谢您的意见。我删除了 # 以测试 Keith 的理论。删除它不会影响错误。我还从文件中删除了 .json 扩展名。同样的错误。
标签: jquery ajax json syntax-error