0

I am trying to get data from Json file with getJson function:

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

But I am not getting anything. When I ran this code in Chrome console developer tool, it only shows this:

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

This is 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
    [Your code works](https://jsfiddle.net/jsucwvwe/). Something else is going on. – Andy Mar 30 '16 at 11:45
  • @Andy it's something related to $.getJSON, but I can't figure out –  Mar 30 '16 at 11:47
  • http://stackoverflow.com/questions/18637418/trying-to-load-local-json-file-to-show-data-in-a-html-page-using-jquery – Andy Mar 30 '16 at 11:50
  • [Here's a fiddle demonstrating that your code indeed works](https://jsfiddle.net/whr76x7r/). JSFiddle requires that echoed JSON be POSTed, so just ignore that minor difference. – André Dion Mar 30 '16 at 12:18

1 Answers1

-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;
        });
    }
});
Umair Khan
  • 283
  • 3
  • 13
  • You need to explain your answer as to why it's a solution for the given issue... This really doesn't look any different from the originally posted code, aside for some reason you made this a POST request. – André Dion Mar 30 '16 at 12:05
  • this is making the array for the locations – Umair Khan Mar 30 '16 at 13:53