1

I had this piece of code:

$.getJSON("https://nycdatastables.s3.amazonaws.com/2013-08-19T18:22:23.125Z/community-districts-polygon.geojson", function(data)
{
    // Add GeoJSON layer to the map once the file is loaded
    geojson = L.geoJson(data,  
    {
        style: style,
        onEachFeature: onEachFeature
    }).addTo(map);
});

Which load some data into a map. However, I want to keep the information, and since $.getJSON is asynchronous., I changed it to $.ajax with the proper parameters:

$.ajax({async: false, url: "https://nycdatastables.s3.amazonaws.com/2013-08-19T18:22:23.125Z/community-districts-polygon.geojson", success: function(data)
{
    // Add GeoJSON layer to the map once the file is loaded
    geojson = L.geoJson(data,  
    {
        style: style,
        onEachFeature: onEachFeature
    }).addTo(map);
}});

The first piece of code worked fine. However, since I changed the method I get:

Error: Invalid GeoJSON object.
geometryToLayerleaflet.js:7:18482
addDataleaflet.js:7:17049
initializeleaflet.js:7:16778
eleaflet.js:5:2544
geoJsonleaflet.js:7:20518
successmap.js:38
jjquery-2.1.0.min.js:1:26681
fireWithjquery-2.1.0.min.js:1:27490
xjquery-2.1.0.min.js:3:10523
(anonymous function)jquery-2.1.0.min.js:3:14160
send
sendjquery-2.1.0.min.js:3:14347
ajaxjquery-2.1.0.min.js:3:9975
loadNeighborhoodsmap.js:35
(anonymous function)index.html:106

This just happens with $.ajax.

pceccon
  • 9,379
  • 26
  • 82
  • 158
  • I'm confused as to why you would need to switch to synchrous operation for "keeping the information" You could simply declare a variable before executing `$.getJSON` for example: `var store;` and then assign the data to the `store` variable in your `$.getJSON` success callback: `$.getJSON('//example.org', function (data) { store = data; });` – iH8 Dec 09 '15 at 19:17

1 Answers1

2

jQuery.getJSON() is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

So in your ajax call you forgot the dataType parameter.

gaetanoM
  • 41,594
  • 6
  • 42
  • 61
  • It worked indeed, @gaemaf. But I've already used it without the `dataType` field and it worked, do you know why? Thank you. – pceccon Dec 09 '15 at 19:05
  • 2
    As @gaemaf mentioned, `$.getJSON` is a shorthand function which in fact is nothing more than a `$.ajax` request with the `dataType` parameter set to `json`. It is clearly stated in the documentation for `$.getJSON`See: http://api.jquery.com/jquery.getjson/ – iH8 Dec 09 '15 at 19:12
  • @iH8: http://stackoverflow.com/questions/1739800/variables-set-during-getjson-function-only-accessible-within-function. I have this problem. – pceccon Dec 09 '15 at 19:24