json - 加载 JSON 文件时,请求的资源上不存在 'Access-Control-Allow-Origin' header 错误

标签 json d3.js polymaps

我的数据集托管在我的保管箱帐户上,但不是调用它的 Javascript 文件。
我正在使用 D3 和 Polymaps 来可视化数据,但是我收到一条错误消息:“XMLHttpRequest 无法加载 https://www.dropbox.com/s/89adzt973quosda/solaruse.json。请求的资源上不存在“Access-Control-Allow-Origin” header 。Origin 'null' 是因此不允许访问。”
(保管箱文件的链接有效,因此您可以查看)

这是我用来加载 JSON 文件的代码(我正在本地开发站点)
从这一点上我不确定该怎么做。

var po = org.polymaps;
        //Create map object, append to #map
        var map = po.map()
            .container(d3.select("#map").append("svg").node())
            .zoom(4)
            .add(po.interact());
        // Add the CloudMade image tiles as a base layer…
        map.add(po.image()
            .url(po.url("http://{S}tile.cloudmade.com"
            + "/1a1b06b230af4efdbb989ea99e9841af" // http://cloudmade.com/register
            + "/998/256/{Z}/{X}/{Y}.png")
            .hosts(["a.", "b.", "c.", ""])));
        // Add the compass control on top.
        map.add(po.compass()
            .pan("none"));
        // Add the custom locations/acres
        d3.json("https://www.dropbox.com/s/89adzt973quosda/solaruse.json", function(data){
            // Insert layer beneath the compass.
            var layer = d3.select("#map svg").insert("svg:g", ".compass");
            // Add an svg:g for each Name.
            var marker = layer.selectAll("g")
                .data(d3.entries(data))
                .enter().append("svg:g")
                .attr("transform", transform);
            // Adding the circles
            marker.append("svg:circle")
            //function scraping the acres from the dataset
            .attr()
        });

最佳答案

问题

发生这种情况是因为 Dropbox 不允许通过其正常域进行跨源请求。见 the MDN CORS docs有关跨源请求的更多信息。

解决方案

您应该可以使用 Dropbox HTTP api (特别是 get file 资源)。或者,您可以使用 Dropbox JS api如果你想使用 JS 函数而不是 http 请求。

或者,正如您在评论中提到的,您可以通过托管自己的 JS 文件来简单地避免使用 Dropbox 😁

关于json - 加载 JSON 文件时,请求的资源上不存在 'Access-Control-Allow-Origin' header 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20361893/

相关文章:

json - JMeter CLI 报告生成失败 - org.apache.jmeter.report.dashboard.GenerationException : Data exporter "json"

java - 序列化已经有 POJO 的 id (java.lang.String)

iphone - 添加小数点后的字典上的ios双引号

javascript - D3 中的可重用函数

javascript - 如何使用重力沿弯曲路径制作动画

d3.js - 用 SI 前缀格式化数字,固定小数位数

JavaScript Polymaps 库 : Get mouse coordinates

java - 如何使用 GSON 从 Primeface 应用程序下载 json 文件?