sen*_*nfo 1 javascript jquery parsing json
我正在尝试使用jQuery解析JSON,我遇到了问题.使用下面的代码,数据不断返回null:
<!DOCTYPE html>
<html>
<head>
<title>JSON Test</title>
</head>
<body>
<div id="msg"></div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$.ajax({
url: 'http://datawarehouse.hrsa.gov/ReleaseTest/HGDWDataWebService/HGDWDataService.aspx?service=HC&zip=20002&radius=10&filter=8357&format=JSON',
type: 'GET',
dataType: 'json',
success: function(data) {
$('#msg').html(data[0].title); // Always null in Firefox/Chrome. Works in IE8.
},
error: function(data) {
alert(data);
}
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
JSON结果如下所示:
{"title":"HEALTHPOINT TYEE CAMPUS","link":"http://www.healthpointchc.org","id":"tag:datawarehouse.hrsa.gov,2010-04-29:/8357","org":"HEALTHPOINT TYEE CAMPUS","address":{"street-address":"4424 S. 188TH St.","locality":"Seatac","region":"Washington","postal-code":"98188-5028"},"tel":"206-444-7746","category":"Service Delivery Site","location":"47.4344818181818 -122.277672727273","update":"2010-04-28T00:00:00-05:00"}Run Code Online (Sandbox Code Playgroud)
如果我代替我的网址与Flickr的API URL(http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=? ),我得到一个有效的JSON结果,我能够利用.
我已经在JSONLint上成功验证了我的JSON ,所以我已经没想到我可能做错了什么.
有什么想法吗?
更新:我让客户端将内容类型切换到application/json.不幸的是,我仍然遇到了完全相同的问题.我还更新了我的HTML并包含了我一直在使用的实时URL.
更新2:我在IE8中尝试了一下,它运行正常.出于某种原因,它在Firefox 3.6.3或Chrome 4.1.249.1064(45376)中都不起作用.我确实注意到返回数据的错误(开发人员正在返回一组数据,即使对于总是会返回单个记录的查询),但它仍然让我感到困惑,为什么它在其他浏览器中不起作用.
值得注意的是,我正在使用本地文件系统上的HTML文件.我认为这可能是一个XSS问题,但这并不能解释Flickr的工作原理.