中止/取消/放弃角度js中的http请求并再次重试 [英] Abort/cancel/abandon a http request in angular js and retry it again
问题描述
我正在处理一个应用程序,如果某个时间过去,我必须停止收听请求,就像在60秒内没有来自请求的响应那样我必须中止该请求,并且一次有多个请求。我不知道如何用中止时间(60秒)跟踪每个请求。我的意思是我怎么知道哪个请求会中止。
I am working on an application in which i have to stop listening to a request if certain time is passed like if there is no response from a request in 60 secs then i have to abort that request and also there are multiple requests are going at a time. I don't know know how to keep track of each request with abort time(60 secs). I mean how would i know which request is to abort.
我想在我的 AngularJS 拦截器中实现这个功能。我已经尝试了许多博客,并发布声称实现此功能,但没有任何帮助我
I want to implement this functionality in my AngularJS interceptor. I have tried many blogs and post that claims to achieve this functionality but nothing helps me
这里是我的拦截器代码
$httpProvider.interceptors.push(['$cookies', '$rootScope', '$q', '$localStorage', '$sessionStorage','$timeout', function ($cookies, $rootScope, $q, $localStorage, $sessionStorage, $timeout) {
return {
'request': function (config) {
config.headers = config.headers || {};
if (typeof config.data !== 'object') {
config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
config.headers.Accept = 'application/json;odata=verbose';
config.headers['X-Requested-With'] = 'XMLHttpRequest';
var token = $localStorage.authToken || $sessionStorage.authToken;
if (token) {
config.headers.Authorization = 'Bearer ' + token;
}
return config;
},
'responseError': function (response) {
var status = response.status;
var error = '';
if(response.data.error) {
error = response.data.error;
}
if(status === 401) {
$rootScope.unauthorizedLogout();
} else if(status === 400 && (error === 'token_invalid' || error === 'token_not_provided')) {
$rootScope.unauthorizedLogout();
}
return $q.reject(response);
}
};
}]);
我做过 $ q.defer
它是取消请求的解决方法,但它并没有帮助我在应用程序中实现我想要的功能。
I do tried the $q.defer
and it's resolve method to cancel request but it's not helping me to achieve the functionality i want in application.
和平 V
推荐答案
设置递增超时值以请求
config.timeout = incrementalTimeout;
处理响应错误的拦截器应如下所示
Your interceptor for handling response error should look like this
'responseError': function (response) {
var status = response.status;
var error = '';
//functionality to for request aborting and resending
if (status === -1 || status >= 500) {
if (attempts <= 3) {
attempts += 1;
return retryRequest(response.config);
}
} else {
// set incrementalTiemout and attempts to default value
incrementalTimeout = 4000;
attempts = 1;
}
if(response.data.error) {
error = response.data.error;
}
if(status === 401) {
$rootScope.unauthorizedLogout();
} else if(status === 400 && (error === 'token_invalid' || error === 'token_not_provided')) {
$rootScope.unauthorizedLogout();
}
return $q.reject(response);
}
在拦截器中重新发送请求的代码
code to resend request in your interceptor
// default request timeout is of 4 seconds and attempt is 1
var incrementalTimeout = 4000;
var attempts = 1;
function retryRequest (httpConfig) {
var thisTimeout = incrementalTimeout;
incrementalTimeout += 1000;
return $timeout(function() {
var $http = $injector.get('$http');
return $http(httpConfig);
}, thisTimeout);
}
这篇关于中止/取消/放弃角度js中的http请求并再次重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!