我想加载外部json文件并将其与d3.js结合使用,但似乎无法加载(我无法在浏览器中看到us-states.json文件)该文件,我找不到任何相关的错误。这里是我的代码: -无法使用d3.js加载外部json文件
我收到此错误: - ActionController::RoutingError (No route matches [GET] "/us-states.json"):
$(document).ready(function() {
//Width and height
var w = 500;
var h = 300;
//Define map projection
var projection = d3.geo.albersUsa()
.translate([w/2, h/2])
.scale([500]);
//Define path generator
var path = d3.geo.path()
.projection(projection);
//Create SVG element
var svg = d3.select("#testing")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load in GeoJSON data
// json = JSON.parse("us-states.json");
// var json = {"my": "json"};
d3.json("us-states.json", function(error, json) {
if (error) return console.warn(error);
//Bind data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", "steelblue");
});
});
文件US-state.json是在我的javascript文件是同一个文件夹。
更新
错误在JavaScript控制台是这样的: -
Failed to load resource: the server responded with a status of 404 (Not Found)
在文件US-states.json
附截图显示的文件是在项目文件夹: -
你是什么意思“它似乎并没有加载”? –
@LarsKotthoff我的意思是我无法在浏览器中看到us-states.json文件 –
这意味着它不在那里。仔细检查您是否已将其上传到正确的地方。 –