How to cancel an $http request in AngularJS?

前端 未结 8 1028
面向向阳花
面向向阳花 2020-11-22 09:49

Given a Ajax request in AngularJS

$http.get(\"/backend/\").success(callback);

what is the most effective way to cancel that request if anot

8条回答
  •  日久生厌
    2020-11-22 10:34

    This feature was added to the 1.1.5 release via a timeout parameter:

    var canceler = $q.defer();
    $http.get('/someUrl', {timeout: canceler.promise}).success(successCallback);
    // later...
    canceler.resolve();  // Aborts the $http request if it isn't finished.
    

提交回复
热议问题