我有一个像这样的网址的WMS查询。
此网址返回JSON结果parseResponse( ...json...)
我想创建angularjs $ http jsonp,但它不起作用。
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);
});
}
这会导致错误" 未定义parseResponse "但我定义了这个功能
答案 0 :(得分:1)
你可以尝试在params中设置 format_options ,因为angularjs的默认回调函数名是 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'
},
};
答案 1 :(得分:0)
以下是使用jsonp
访问AngularJS回调结果的方法:
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
}
}
我在url(REQUEST=GetFeatureInfo
)上取下了一个重要的参数,并将其包含在jsonp
函数调用的params参数中,以显示params
参数的示例通过了。