我试图用的getJSON函数来获取从JSON文件数据:的getJSON然后不工作
$.getJSON("uploads/data.json").then(function(data) {
allData = data.sculptures;
locations = {};
for(var i=0; i<data.location.length; i++) {
locations[data.location[i].name] = data.location[i].location;
}
console.log(allData);
console.log(locations);
});
,但我没有得到任何东西。当我运行在Chrome控制台开发工具的代码,那只能说明这一点:
Object {}
always:()
done:()
fail:()
pipe:()
progress:()
promise: (a)
state:()
then:()
__proto__: Object
这是data.json:
{"sculptures":[{"title":"Bust of Caracalla","location":"Paris"},
{"title":"Brutus The Younger","location":"Paris"}],
"location":[{"name":"Beaux-Arts in Dijon, France","location":{"lat":47,"lng":5}},
{"name":"Louvre, Paris","location":{"lat":48,"lng":2}},
{"name":"St Pauls Cathedral, London","location":{"lat":51,"lng":-0}},
{"name":"V&A, London","location":{"lat":51,"lng":-0}}]}
[你的代码工作(https://开头的jsfiddle。净/ jsucwvwe /)。其他事情正在发生。 – Andy
@Andy它与$ .getJSON有关,但我无法弄清楚 – julie
http://stackoverflow.com/questions/18637418/trying-to-load-local-json-file-to-show-data-in -a-html-page-using-jquery – Andy