2014-12-01 61 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,而是將