我試圖用的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