如果我从浏览器向服务器发出此调用:
http://localhost:8080/api/items/number/all.json
或者来自卷曲:
curl -G http://localhost:8080/api/items/number/all.json
我返回以下 json:
{
"language": null,
"number": 10,
"queryId": 0,
"from": null,
"to": null,
"percentage": 33,
"dataInfoSet": null
}
但是当我使用d3.json
call:
d3.json("http://localhost:8080/api/items/number/all.json", function(jsondata) {
console.log(jsondata);
});
输出来自console.log
is null
.
如果改为 http 调用,我会将 json 保存在文件 (fileWithData.json) 中并执行以下操作:
d3.json("fileWithData.json", function(jsondata) {
console.log(jsondata);
});
一切都按预期进行。有谁知道可能是什么问题?
在 d3-js Goole 小组的帮助下解决了。
问题是加载 json 的页面不是从 localhost:8080 提供的,因此存在跨域限制。我刚刚在同一应用程序中部署了该文件。
如果必须进行跨域调用,该小组建议使用 jasonp 并特别使用 CORS (http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/ http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)