javascript - 无效的 geoJson 对象 - Leaflet 和 Ajax

标签 javascript jquery ajax geojson

我有这段代码:

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

将一些数据加载到 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

这恰好发生在 $.ajax 中。

最佳答案

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

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

因此在您的 ajax 调用中您忘记了 dataType 参数。

关于javascript - 无效的 geoJson 对象 - Leaflet 和 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34186473/

相关文章:

javascript - 使用 Cakefile 编译 CoffeeScript

javascript - 动画循环中的 jQuery .delay()

javascript - 无法在 LightSwitch HTML 中使用 Syncfusion JavaScript 组件

jquery - Django 框架中的 Ajax Post?

jquery - 如何使用 jQuery 发出 AJAX HTTPS GET 请求

c# - 通过 JQuery 访问 Controller

javascript - 谷歌地图嵌入代码无效

javascript - 状态更改后,React-Redux 连接的组件不会更新

javascript - 从安卓网站打开键盘

javascript - 同源策略