2016-03-30 106 views
0

我試圖用的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}}]} 
+3

[你的代碼工作(https://開頭的jsfiddle。淨/ jsucwvwe /)。其他事情正在發生。 – Andy

+0

@Andy它與$ .getJSON有關,但我無法弄清楚 – julie

+0

http://stackoverflow.com/questions/18637418/trying-to-load-local-json-file-to-show-data-in -a-html-page-using-jquery – Andy

回答

-1
locations = []; 
    $.ajax({ 
    type: "POST", 
    url: "uploads/data.json", 
    dataType: "json", 
    success: function(data) { 
     $(data.location).each(function(i, location){ 
      locations[data.location[i].name] = data.location[i].location; 
     }); 
    } 
}); 
+0

你需要解釋你的答案,爲什麼它是給定問題的解決方案......除了出於某種原因,你做了這個POST請求之外,這與真正的發佈代碼沒有任何區別。 –

+0

這是製作位置的陣列 –