使用javascript jsonp回调函数

使用javascript jsonp回调函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我们在使用javascript时使用JSONP方法,以避免跨域问题 但我无法理解其中的逻辑。我使用的服务返回如下结果: parseResponse({"type":"FeatureCollection",......}) $.ajax({ type: 'GET', url: url, dataType: 'json', success: function(json) { $(".test").html(json.type); },

我们在使用javascript时使用JSONP方法,以避免跨域问题

但我无法理解其中的逻辑。我使用的服务返回如下结果:

parseResponse({"type":"FeatureCollection",......})
$.ajax({
    type: 'GET',
    url: url,
    dataType: 'json',
    success: function(json) {
        $(".test").html(json.type);
    },
    error: function(e) {
       console.log(e);
    }
});
服务链接如下:

我在JQuery ajax请求中使用它,如下所示:

parseResponse({"type":"FeatureCollection",......})
$.ajax({
    type: 'GET',
    url: url,
    dataType: 'json',
    success: function(json) {
        $(".test").html(json.type);
    },
    error: function(e) {
       console.log(e);
    }
});
这种ajax总是在错误函数中工作。但请求结果正在实现。如何捕获parseResponse回调函数


这是我的工作代码

,因为服务默认返回jsonp响应,即使没有自定义回调参数,我找到的最快解决方案是从错误中获取响应文本并对其进行评估:

var url = "http://giswebservices.massgis.state.ma.us/geoserver/wms?VERSION=1.1.1&REQUEST=GetFeatureInfo&LAYERS=massgis:GISDATA.ACECS_POLY&SRS=EPSG:26986&BBOX=11830.0,776202.9449152543,348201.0,961492.0550847457&WIDTH=708&HEIGHT=390&INFO_FORMAT=text/javascript&FEATURE_COUNT=100&QUERY_LAYERS=massgis:GISDATA.ACECS_POLY&X=120&Y=109&FORMAT&STYLES=&SERVICE=WMS";

function parseResponse( data ) {
    console.log( 'parse', data );
}

$.ajax({
    type: 'GET',
    url: url,
    dataType: 'json',
    success: function(json) {

    },
    error: function(e) {
        eval(e.responseText);
    }
});

因为这是JSONP,所以必须将
数据类型设置为
'JSONP'
,而不是
'json'
。请参阅我的服务链接直接返回我的_回调({…})结果,它不需要回调参数。所以我可以像使用JSONP一样使用它吗