我正在研究 D3 graphs构建一些交互式仪表板。我可以使用静态 JSON 集成图形。现在我想从我正在运行的服务器获取 JSON。我的服务器需要某种基本授权。
我可以在 Restclient 中获得 XML 响应添加基本身份验证 header 后。但在 D3 中它不起作用。
我的 D3 看起来像这样......
d3.json("http://localhost:9001/***/rest/products", function(error, root) {
var node = svg1.selectAll(".node")
.data(bubble.nodes(classes(root))
.filter(function(d) { return !d.children; }))
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("title")
.text(function(d) { return d.className + ": " + format(d.value); });
node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return color(d.packageName); });
node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function(d) { return d.className.substring(0, d.r / 3); });
});
它会给出403 Forbidden
错误,因为这里我没有添加Authentication header
。
如何在 D3.json 中添加身份验证 header ,以便我可以访问 JSON 形式的资源而不会出现任何权限被拒绝的问题。
最佳答案
您可以使用#header
方法添加请求 header :
https://github.com/mbostock/d3/wiki/Requests#header
d3.json("http://localhost:9001/***/rest/products")
.header("header-name", "header-value")
.get(函数(错误,根){
//你的代码在这里。
})
关于javascript - 如何在 D3 中访问 JSON 时添加授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23993671/