我有一个小的javascript Web应用程序,它以json格式读取硬编码字符串,如下所示(这是在脚本本身内):
root = {
"name": "John", "imageURL":"images/root.png","id":"1",
"children": [
{"name": "project", "imageURL":"cool.png","id":"2",
"children":[{"name": "ideas", "imageURL":"object1.png","id":"3"},
{"name": "boards", "imageURL":"object2.png","id":"46"}
]},
]
};
然后D3.js使用它来渲染其图像。相反,我想从位于服务器上的file.json(与Web应用程序相同的目录)中读取此json。我想我必须使用AJAX。我试过这个:
var output = "";
$.ajax({
url: "file.json",
type: 'Get',
dataType: "json",
success: function (result) {
output = result;
}
});
但没有任何东西来自于此。
答案 0 :(得分:1)
file.json需要是一个有效的json文件,替换它的内容:
{
"name": "John", "imageURL":"images/root.png","id":"1",
"children": [
{"name": "project", "imageURL":"cool.png","id":"2",
"children":[{"name": "ideas", "imageURL":"object1.png","id":"3"},
{"name": "boards", "imageURL":"object2.png","id":"46"}
]}
]
}
答案 1 :(得分:1)