简体   繁体   中英

how to access angularjs callback result jsonp

I have a WMS query with url like this.

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=10&QUERY_LAYERS=massgis:GISDATA.ACECS_POLY&X=120&Y=109&FORMAT&STYLES=&SERVICE=WMS

This url returns JSON result parseResponse( ...json...)

I want to create angularjs $http jsonp but it did not work.

function appCtrl($scope, $http){
    function parseResponse(data) {
        $scope.data = data
    } 

    var httpOptions = {
        url: "http://giswebservices.massgis.state.ma.us/geoserver/wms?VERSION=1.1.1&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=10&QUERY_LAYERS=massgis:GISDATA.ACECS_POLY&X=120&Y=109&FORMAT&STYLES=&SERVICE=WMS",
        method: "JSONP",
        params : {
            REQUEST: 'GetFeatureInfo',
        },
    };

    $http(httpOptions).
        success(function(data){
            console.log(data);
            $scope.data = data;
        }).
        error(function(data){
            console.log(data);
        });
}

This gives error " parseResponse is not defined " But I defined that function

You can try set format_options in params, Because angularjs default callback function name is JSON_CALLBACK

var httpOptions = {
    url: "http://giswebservices.massgis.state.ma.us/geoserver/wms?VERSION=1.1.1&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=10&QUERY_LAYERS=massgis:GISDATA.ACECS_POLY&X=120&Y=109&FORMAT&STYLES=&SERVICE=WMS",
    method: "JSONP",
    params : {
        REQUEST: 'GetFeatureInfo',
        format_options: 'callback: JSON_CALLBACK'
    },
};

Here is how to access your AngularJS callback result using jsonp :

http://jsfiddle.net/jCUSh/138/

function WMSCtrl($scope, $http) {
    $scope.nums = [1,2,3]
    $scope.data = null;
    $scope.get_data = function() {
      var url2 = 'http://giswebservices.massgis.state.ma.us/geoserver/wms?VERSION=1.1.1&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=10&QUERY_LAYERS=massgis:GISDATA.ACECS_POLY&X=120&Y=109&FORMAT&STYLES=&SERVICE=WMS'
      $http.jsonp(url2, {params : {REQUEST: 'GetFeatureInfo'}});
    }

    window.parseResponse = function(data) {
      $scope.data = data
    }
}

I took off a vital param on the url (which was REQUEST=GetFeatureInfo ) and included it in the params argument on the jsonp function call to show an example of how the params arguments is passed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM