[英]Abort pending ajax request in AngularJS
How can I abort my pending ajax
requests in AngularJS
? 如何在
AngularJS
终止我的未决ajax
请求? I am using a Service
for making an ajax call and want to abort all pending requests if a new one is made on the same route. 我正在使用
Service
进行ajax调用,并且如果在同一路由上进行了新请求,则想中止所有未决请求。
Here is my service: 这是我的服务:
myService.service('angService', function($http){
this.getfromRemote=function(url){
return $http.get(location+'/dev/apiv-1-0-2/'+url);
}
});
Here I am calling the service. 我在这里打电话给服务。 Before running next, I want to abort the pending ajax requests.
在接下来运行之前,我想中止未决的ajax请求。
angService.getfromRemote('paymentStatement/'+$scope.username)
.success(function(response){
})
To abort a pending $http
request, you can pass a promise to the timeout property of the $http
request, and call resolve
to abort the request. 要中止挂起
$http
请求时,你可以通过承诺给的超时属性$http
请求和呼叫resolve
中止请求。
myService.service('angService',function($http, $q){
var pending = false;
var deferred = $q.defer();
this.getfromRemote=function(url){
if (pending) {
deferred.resolve();
deferred = $q.defer();
}
pending = true;
return $http({
method: 'GET',
url: location+'/dev/apiv-1-0-2/'+url,
timeout = deferred.promise;
}).then(function(result) {
pending = false;
deferred = $q.defer();
return result;
}, function(result) {
pending = false;
deferred = $q.defer();
return result;
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.