2014-12-01 88 views
1

我正在使用Angular版本1.2.1,并试图在同一类型的新请求初始化时在我的应用程序中实现取消。我使用了代码示例here,但我无法使其工作。我认为这是由于我使用$ http.jsonp()。

当我取消一个jsonp请求时,.then回调从不会被调用(如预期的那样),但是请求不会被取消,并且当响应到来时,控制台中会出现一个未定义的错误(jsonp回调函数不再存在)。

如何避免出现此控制台错误?是否有可能取消jsonp请求alltogheter?

http://jsfiddle.net/HB7LU/8668/

HTML:

<div ng-controller="MyCtrl"> 
    <span ng-bind="status"></span> 
    <hr> 
    <a href ng-click="doStuff()"> 
     Start 
    </a> 
    <hr> 
    <a href ng-click="cancel()"> 
     Cancel 
    </a> 
</div> 

JS:

var myApp = angular.module('myApp',[]); 
function MyCtrl($scope, $q, $http) { 
    $scope.status = "Idle"; 

    $scope.doStuff = function() { 
     var canceller = $q.defer(); 
     $scope.status = "Ongoing"; 
     $scope.getting = $http.jsonp('http://cruisebookingapi.ving.se/cruisePackage/search/1/STO/141220/false/20141211/42,42/-1?callback=JSON_CALLBACK', {timeout: canceller.promise}); 
     $scope.getting.then(function(data) { 
      $scope.status = "Finished"; 
      console.log(data); 
     }); 
     $scope.cancel = function() { 
      $scope.status = "Cancelled"; 
      canceller.resolve('Cancel all the things'); 
     }; 
    }; 
} 
+1

我觉得你真的是到了一些东西......这应该取消 – SoluableNonagon 2014-12-01 17:58:14

+1

JSONP不使用XMLHttpRequest,而是将