1
if (xhr != null) { // xhr is AJAX call request object.
    console.log("abort");
    canceler.resolve();
}

xhr = $http({ method: 'POST', url:("/Home/GetLibrary"), timeout: canceler.promise })
    .success(){
        alert("success");
    })
    .error() {
        alert("error");
    });

Here I check the xhr object. If xhr object is not null then I abort the call. But it aborts the current call, not the previous Ajax call.
I try to call the same function again, then all subsequent AJAX call are aborted.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
chirag
  • 1,818
  • 1
  • 15
  • 36

2 Answers2

1

try this

 var canceller = $q.defer();
     
    $http.get("yourUrl", { data:data })
         .then(function(response){
            $scope.movie = response.data;
        });
     
    $scope.cancel = function(){
        canceller.resolve("user cancelled");  
    };

$scope.cancel();

Harutyun Abgaryan
  • 2,013
  • 1
  • 12
  • 15
0

You can do it through promises. Have a look at Aborting AJAX Requests Using $http And AngularJS, the guy explained the topic very well.

Also check the following question :

How to cancel an $http request in AngularJS?

Community
  • 1
  • 1
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101