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.
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
here is my interceptor code
$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);
}
};
}]);
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.
Peace out V