在解析內聯JSON對象d3.json(obj, function(error, root)
時,它在本地運行時正常工作,但是當我在Tomcat服務器上運行時,出現XMLHttpparse錯誤。我通過互聯網搜索。我發現答案是CORS。但是如何實現這一點並不明確。你可以幫我嗎?D3.js JSON解析錯誤
var obj = {
"name": "vis",
"children": [
{
"name": "Votes",
"children": [
{"name": "200", "size": 200,"url":"1"},
{"name": "500", "size": 500,"url":"2"},
{"name": "300", "size": 300,"url":"3"},
{"name": "400", "size": 400,"url":"4"}
]
},
{
"name": "Reputation",
"children": [
{"name": "200", "size": 200},
{"name": "500", "size": 500},
{"name": "300", "size": 300},
{"name": "400", "size": 400}
]
},
{
"name": "Accepted Answer",
"children": [
{
"name": "encoder",
"children": [
{"name": "Accepted Answer", "size": 500}
]
}
]
}
]
};
d3.json(obj, function(error, root) {
alert('error '+error);
alert('root: '+root);
if (error) return console.log(error);
}
如果'obj'已經是一個JavaScript對象,爲什麼你需要'JSON.parse(obj)'? – Oleg
@ Oleg好的,你可能不需要那樣做。我刪除了那個珍聞。 – aug