将jQuery AJAX请求传递给D3

时间:2015-04-29 18:24:18

标签: javascript jquery ajax json d3.js

我正在尝试从基于链接的URL获取JSON,并发送我的D3图表使用的JSON对象。该URL直接指向JSON。但是,我收到错误root.links.forEach未定义。当我在本地发送JSON而不是通过AJAX请求发送代码时,代码可以正常工作。不知道为什么root.links.forEach未定义是我收到的错误。

代码:

var width = innerWidth,
    height = innerHeight,
    color = d3.scale.category20(),
    root;

... (random code) ...

$.ajax({
type: "GET",
url: url + key,
crossDomain: true,
async: true,
dataType: "json",
success: function(data) {
    root = data;
    update();
}
});

function update() {

// sets the source and target to use id instead of index
var edges = [];
root.links.forEach(function(e) {

... (rest of code) ...

如果我拿走AJAX电话并用它替换它,那么它可以工作......

d3.json("/path/to/json.json", function(error, json) {
    // expands scope of json
    root = json
    update();
});

0 个答案:

没有答案