我实现了authInterceptorService,所以当响应未授权时,我发送新令牌请求,然后我想重新发送previoes请求。所以我需要将它存储在某处并重新发送,最好的方法是什么?
var _responseError = function (rejection) {
if (rejection.status === 401) {
var authService = $injector.get('authService');
var authData = localStorageService.get('authorizationData');
if (authData) {
authService.refreshToken().then(function (response) {
//redirect to original request
},
function (err) {
$location.path('/login');
});
}
authService.logOut();
$location.path('/login');
}
return $q.reject(rejection);
}
答案 0 :(得分:1)
为什么不自己提出要求?
// define the original request
var originalRequest = function( some input ){
$http.post( 'url' , data ).then(onSuccess, _responseError);
}
然后在你的_responseError函数中,只需调用原始请求
var _responseError = function (rejection) {
if (rejection.status === 401) {
var authService = $injector.get('authService');
var authData = localStorageService.get('authorizationData');
if (authData) {
authService.refreshToken().then(function (response) {
//redirect to original request
originalRequest( some input );
},
function (err) {
$location.path('/login');
});
}
authService.logOut();
$location.path('/login');
}
return $q.reject(rejection);
}
此外,如果失败,您会获得请求的原始配置,因此您可以使用该...
if (authData) {
authService.refreshToken().then(function (response) {
//redirect to original request
$http.post(rejection.config).then( ... , ... );
},
function (err) {
$location.path('/login');
});
}