JSONP没有按预期在AngularJS中工作

时间:2022-12-06 19:45:17

The first commented $scope.url which I took from angular documentation works fine, similarly I am trying to invoke another public url which gives me back the response but always goes to error callback.

我从angular文档中获取的第一个注释$ scope.url工作正常,同样我试图调用另一个公共URL,它给了我回复响应但总是进行错误回调。

Both the URLs are in public domain, so please feel free to try.

这两个网址都属于公共领域,请随时尝试。

                $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);
                        });
                };

JSONP没有按预期在AngularJS中工作

1 个解决方案

#1


2  

For some reason dots are stripping from callbacke method name http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?callback=angular.callbacks._0&mainSearchCriteria.v.cs=2.16.840.1.113883.6.96&mainSearchCriteria.v.c=41381004&knowledgeResponseType=application/javascript

由于某种原因,点从callbacke方法名称http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?callback=angular.callbacks._0&mainSearchCriteria.v.cs=2.16.840.1.113883.6.96&mainSearchCriteria中删除。 VC = 41381004&knowledgeResponseType =应用/ JavaScript的

This response can’t be handle by AngularJS as Angular wants to call angular.callbacks.0

当Angular想要调用angular.callbacks.0时,AngularJS无法处理此响应

Some development background

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;
});

Every URL that has JSON_CALLBACK string inside will be replaced by angular.callbacks.{\D}

内部包含JSON_CALLBACK字符串的每个URL都将被angular.callbacks替换。{\ D}

source: AngularJS.js

#1


2  

For some reason dots are stripping from callbacke method name http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?callback=angular.callbacks._0&mainSearchCriteria.v.cs=2.16.840.1.113883.6.96&mainSearchCriteria.v.c=41381004&knowledgeResponseType=application/javascript

由于某种原因,点从callbacke方法名称http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?callback=angular.callbacks._0&mainSearchCriteria.v.cs=2.16.840.1.113883.6.96&mainSearchCriteria中删除。 VC = 41381004&knowledgeResponseType =应用/ JavaScript的

This response can’t be handle by AngularJS as Angular wants to call angular.callbacks.0

当Angular想要调用angular.callbacks.0时,AngularJS无法处理此响应

Some development background

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;
});

Every URL that has JSON_CALLBACK string inside will be replaced by angular.callbacks.{\D}

内部包含JSON_CALLBACK字符串的每个URL都将被angular.callbacks替换。{\ D}

source: AngularJS.js