2016-03-30 12 views
0

arbeitet Ich versuche, Daten von Json-Datei mit getJSON Funktion zu erhalten:getJSON dann nicht

$.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); 
}); 

Aber ich bin nicht immer alles. Wenn ich diesen Code in Chrome Konsole Entwickler-Tool lief, es zeigt nur das:

Object {} 
     always:() 
     done:() 
     fail:() 
     pipe:() 
     progress:() 
     promise: (a) 
     state:() 
     then:() 
     __proto__: Object 

Diese data.json ist:

{"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

[Ihr Code funktioniert] (https: // jsfiddle. net/jsucwvwe /). Etwas anderes passiert. – Andy

+0

@Andy es ist etwas mit $ .getJSON verwandt, aber ich kann nicht herausfinden, – julie

+0

http://StackOverflow.com/Questions/18637418/trying-to-load-local-Json-File-to-show-data-in -a-html-page-using-jquery – Andy

Antwort

-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

Sie müssen Ihre Antwort erklären, warum es eine Lösung für das gegebene Problem ist ... Das sieht wirklich nicht anders aus als der ursprünglich gepostete Code, abgesehen davon, dass Sie aus irgendeinem Grund eine POST-Anfrage gestellt haben. –

+0

Dies macht das Array für die Standorte –