Javascript 请求JSONP的多个AJAX调用的问题

Javascript 请求JSONP的多个AJAX调用的问题,javascript,jquery,ajax,asynchronous,Javascript,Jquery,Ajax,Asynchronous,我有一个代码,我从geoserver请求数据并在地图上显示geoJSON。对于这个请求,我使用了三个AJAX调用,如下所示: //JSON request var WFSLayer1 = null; var ajax = $.ajax({ url: "http://localhost:8080/geoserver/PastDenver/ows?service=WFS&version=2.0.0&

我有一个代码,我从geoserver请求数据并在地图上显示geoJSON。对于这个请求,我使用了三个AJAX调用,如下所示:

//JSON request   
            var WFSLayer1 = null;
            var ajax = $.ajax({
                url: "http://localhost:8080/geoserver/PastDenver/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=PastDenver:dataset3&maxFeatures=300&outputFormat=text/javascript&format_options=callback:getJson",
                dataType: 'jsonp',
                jsonpCallback: 'getJson',
                success: function (response) {
                    WFSLayer1 = L.geoJson(response, {
                        style: function (feature) {
                            return {
                                weight: 5,
                                color: '#6e7ce8',
                                weight: 2,
                                opacity: 1,
                                dashArray: '3',
                                fillOpacity: 0.7,
                            };
                        },
                        onEachFeature: function (feature, layer) {
                            popupOptions = {
                                maxWidth: 200
                            };
                            if (feature.properties.name !== null) {
                                layer.bindLabel(feature.properties.name, popupOptions, {
                                    noHide: true
                                });
                            };
                            layer.bindPopup("<b>Name: </b>" + feature.properties.name + "<br><b>Description: </b>" + feature.properties.descr + "<br><b>Floors: </b>" + feature.properties.floors + "<br><b>Material: </b>" + feature.properties.material);
                            layer.on({
                                click: zoomToFeature
                            })
                        }
                    }).addTo(dataset1);
                }
            });

            var WFSLayer2 = null;
            var ajax = $.ajax({
                url: "http://localhost:8080/geoserver/PastDenver/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=PastDenver:dataset1&maxFeatures=300&outputFormat=text/javascript&format_options=callback:getJson",
                dataType: 'jsonp',
                jsonpCallback: 'getJson',
                success: function (response) {
                    WFSLayer1 = L.geoJson(response, {
                        style: function (feature) {
                            return {
                                weight: 5,
                                color: '#e31424',
                                weight: 2,
                                opacity: 1,
                                dashArray: '3',
                                fillOpacity: 0.7,
                            };
                        },
                        onEachFeature: function (feature, layer) {
                            popupOptions = {
                                maxWidth: 200
                            };
                            if (feature.properties.name !== null) {
                                layer.bindLabel(feature.properties.name, popupOptions, {
                                    noHide: true
                                });
                            };
                            layer.bindPopup("<b>Name: </b>" + feature.properties.name + "<br><b>Description: </b>" + feature.properties.descr + "<br><b>Floors: </b>" + feature.properties.floors + "<br><b>Material: </b>" + feature.properties.material);
                            layer.on({
                                click: zoomToFeature
                            })
                        }
                    }).addTo(dataset2);
                }
            });

            var WFSLayer3 = null;
            var ajax = $.ajax({
                url: "http://localhost:8080/geoserver/PastDenver/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=PastDenver:dataset2&maxFeatures=300&outputFormat=text/javascript&format_options=callback:getJson",
                dataType: 'jsonp',
                jsonpCallback: 'getJson',
                success: function (response) {
                    WFSLayer3 = L.geoJson(response, {
                        style: function (feature) {
                            return {
                                weight: 5,
                                color: '#14e324',
                                weight: 2,
                                opacity: 1,
                                dashArray: '3',
                                fillOpacity: 0.7,
                            };
                        },
                        onEachFeature: function (feature, layer) {
                            popupOptions = {
                                maxWidth: 200
                            };
                            if (feature.properties.name !== null) {
                                layer.bindLabel(feature.properties.name, popupOptions, {
                                    noHide: true
                                });
                            };
                            layer.bindPopup("<b>Name: </b>" + feature.properties.name + "<br><b>Description: </b>" + feature.properties.descr + "<br><b>Floors: </b>" + feature.properties.floors + "<br><b>Material: </b>" + feature.properties.material);
                            layer.on({
                                click: zoomToFeature
                            })
                        }
                    }).addTo(dataset3);
                }
            });
根据我的研究,在运行多个AJAX调用时,这种类型的错误似乎很常见。我不明白的是为什么这个错误没有100%的发生。什么类型的技术可以用来修复它?我听说了
不同的对象
,但无法将其应用到我的代码中,我在这方面的专业水平还远远不够


虽然这可能倾向于一个GIS问题,但我相信这类问题更多地与普通的jQuery和异步调用相关

来自jQuery文档

jsonpCallback

指定JSONP请求的回调函数名。将使用此值代替jQuery自动生成的随机名称。最好让jQuery生成一个唯一的名称,因为这样可以更容易地管理请求并提供回调和错误处理。当您希望对GET请求启用更好的浏览器缓存时,可能需要指定回调

该函数可能未定义,因为jQuery可能在其自身之后进行清理,并在调用后删除该函数


删除
jsonpCallback:'getJson',
让jQuery自己生成函数名,这样就不会覆盖以前的函数(并在第二次调用之前删除它们)。

哇,你的回答完全解决了我的问题!首先我删除了所有回调,但没有任何效果,然后我只是为每个回调创建了唯一的值。这似乎已经修复了一切!但是对于记录:我如何允许jQuery创建自己的名称?@RicardoOliveira-根据答案的最后一段,通过不告诉它您想要一个特定的名称。
ows?service=WFS&version=2.0.0&request=GetFeature&typeName=PastDenver:dataset1&maxFeatures=300&outpu…:1 Uncaught TypeError: undefined is not a function