如何通过 d3.json 从 S3 下载 json 文件

How to download a json file from S3 via d3.json

我设置了一个 S3 存储桶并上传了一些 json 文件。每个文件都有内容类型:application/json。通过URL访问的文件,如果我在浏览器中输入或使用wget,我可以毫无问题地下载它。

但是如果我尝试通过 d3.json 或 d3.xhr 下载此文件,函数 return null:

d3.json(jsonUrl, function(json) {
    alert(json); 
});

d3.xhr(jsonUrl, function(r) {
    alert(r);
});

我已经在 Firefox 中打开了调试控制台,我可以看到,请求 url 是正确的,它确实通过了,响应是 200,但是响应中没有数据。

更新。结果是什么浏览器允许通过 java 脚本从不同主机下载文件(如 http://www.w3.org/TR/cors/ 中所述)。我在 Apache 中设置了一个代理,目前它可以工作,但我正在寻找更好的解决方案。

您不能这样做,因为浏览器的 'Same Origin Policy' security model. You can achieve it without a proxy by using JSONP

看看上面的示例代码link例如