0
我有一个奇怪的错误,其中D3不会把我的JSON数据引用一个变量,但将采取它,如果我通过控制台日志打印出来并复制粘贴它进入相同的变量。D3意外的价值翻译,不会接受变量
所以JSON的这个源不起作用:
function reformat(data) {
var jsonObject = JSON.parse(data, function(k, v) {
return (typeof v === "object" || isNaN(v)) ? v : parseInt(v, 10);
});
var treeData = (JSON.stringify(makeTree(jsonObject), null, 4));
//etc code for D3
}
但如果我硬是通过控制台打印出来,并宣布它是这样:
treeData = [
{
"id": 1,
"name": "Start",
"children": [
{
"id": 2,
"name": "Decision A",
"children": [
{
"id": 4,
"name": "Possibility A-1",
"children": [
{
"id": 8,
"name": "Consequence A-1"
}
]
},
{
"id": 5,
"name": "Possibility A-2",
"children": [
{
"id": 9,
"name": "Consequence A-2"
}
]
}
]
},
{
"id": 3,
"name": "Decision B",
"children": [
{
"id": 6,
"name": "Possibility B-1",
"children": [
{
"id": 10,
"name": "Consequence B-1"
}
]
},
{
"id": 7,
"name": "Possibility B-2",
"children": [
{
"id": 11,
"name": "Consequence B-2"
}
]
}
]
}
]
}
];
突然工作正常。我无法继续复制并重新声明变量。如果输出是相同的,为什么它不接受它?
这个函数会得到在加载网页叫了一次:
function reqListener() {
console.log(this.responseText);
}
var oReq = new XMLHttpRequest();
oReq.onload = function() {
reformat(this.responseText);
};
oReq.open("get", "getData.php", true);
oReq.send();