我从angular文档中获取的第一个注释$ scope.url工作正常,同样我试图调用另一个公共URL,它给我回复响应但总是进行错误回调。
这两个网址都属于公共领域,因此请随时尝试。
$scope.method = 'JSONP';
// URL 1: Working
// $scope.url = 'https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero';
// URL 2: Not working (Getting response with status 200OK but going to error callback)
$scope.url= 'http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?callback=JSON_CALLBACK&mainSearchCriteria.v.cs=2.16.840.1.113883.6.96&mainSearchCriteria.v.c=41381004&knowledgeResponseType=application/javascript';
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
success(function(data, status) {
console.log(data);
console.log(status);
}).
error(function(data, status) {
console.log(data);
console.log(status);
});
};
答案 0 :(得分:2)
AngularJS 无法处理此响应,因为Angular想要调用angular.callbacks.0
var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
function() {
if (callbacks[callbackId].data) {
completeRequest(callback, 200, callbacks[callbackId].data);
} else {
completeRequest(callback, status || -2);
}
callbacks[callbackId] = angular.noop;
});
其中包含JSON_CALLBACK
字符串的每个网址都将替换为angular.callbacks.{\D}
来源:AngularJS.js