无效的geoJson对象 - Leaflet和Ajax

时间:2015-12-09 18:35:52

标签: javascript jquery ajax geojson

我有这段代码:

Properties p = new Properties();
FileInputStream fs = new FileInputStream("src/main/resources/fileName.properties"));
p.load(fs);

将一些数据加载到地图中。 但是,我想保留这些信息,因为$.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); }); 是异步的。我使用适当的参数将其更改为$.getJSON

$.ajax

第一段代码工作正常。但是,因为我改变了方法,我得到了:

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

这恰好发生在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

1 个答案:

答案 0 :(得分:2)

jQuery.getJSON()是一种简写的Ajax函数,相当于:

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

所以在你的ajax调用中你忘记了dataType参数。

相关问题