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();