I am attempting GET a JSON from a URL based on a link and send that JSON object to be used by my D3 graph. The URL leads directly to the JSON. However, I am getting an error root.links.forEach is undefined. The code works when I am sending in a JSON locally but not through the AJAX request. Not sure why root.links.forEach being undefined is the error I receive.
Code:
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) ...
If I take away the AJAX call and replace it with this then it works...
d3.json("/path/to/json.json", function(error, json) {
// expands scope of json
root = json
update();
});