问题描述
如果我通过浏览器拨打此服务器:或从curl(curl -G )我回到以下json:
If I make this call to the server from a browser: http://localhost:8080/api/items/number/all.json or from curl (curl -G http://localhost:8080/api/items/number/all.json) I get back the following json:
{
"language":null,
"number":10,
"queryId":0,
"from":null,
"to":null,
"percentage":33,
"dataInfoSet":null
}
$ b b
但是,当我使用d3.json调用:
However when I use d3.json call:
d3.json("http://localhost:8080/api/items/number/all.json", function(jsondata){
console.log(jsondata);
});
console.log的输出为空。
the output from console.log is null.
如果改为使用http调用,我将json保存在文件(fileWithData.json)中并执行:
If instead the http call, I save the json in a file (fileWithData.json) and do:
d3.json("fileWithData.json", function(jsondata){
console.log(jsondata);
});
一切正常。有人知道可能是什么问题?
Everything works as expected. Does anyone know what might be the problem?
推荐答案
在d3-js Goole组的帮助下解决。
问题是,加载json的页面不是从localhost:8080提供的,因此,存在跨域限制。我刚刚在同一个应用程序中部署了该文件。
如果必须进行跨域调用,组建议使用jasonp和特别和CORS(
)
Solved with the help of d3-js Goole group.The problem was that the page loading the json was not being served from localhost:8080, thus, there were cross domain restrictions. I just deployed the file within the same application.In case cross domain calls have to be made, the group suggested the use of jasonp and specially and CORS (http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/)
这篇关于d3.json调用总是获取空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!