如何未被授权时,重发请求 [英] How to resend request when not authorized
本文介绍了如何未被授权时,重发请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实现authInterceptorService所以当响应没有授权我送请求新的令牌,然后我要重新发送previoes请求。所以,我需要存储在某个地方,然后重新发送,什么是做的最好的方法是什么?
VAR _responseError =功能(拒绝){
如果(rejection.status === 401){
变种authService = $ injector.get('authService');
变种的authData = localStorageService.get('authorizationData'); 如果(的authData){
authService.refreshToken()。然后(功能(响应){
//重定向到原来的请求
},
功能(错误){
$ location.path('/登录');
});
} authService.logOut();
$ location.path('/登录');
}
返回$ q.reject(拒绝);
}
解决方案
为什么不把它作为自己的要求呢?
//定义原始请求
VAR originalRequest =功能(一些输入){
$ http.post(URL,数据)。然后(的onSuccess,_responseError);
}
然后在您的_responseError功能,只需调用原始请求
VAR _responseError =功能(拒绝){
如果(rejection.status === 401){
变种authService = $ injector.get('authService');
变种的authData = localStorageService.get('authorizationData'); 如果(的authData){
authService.refreshToken()。然后(功能(响应){
//重定向到原来的请求 originalRequest(一些输入); },
功能(错误){
$ location.path('/登录');
});
} authService.logOut();
$ location.path('/登录');
}
返回$ q.reject(拒绝);
}
此外,失败,你得到请求的原始配置,所以你可以只使用...
如果(的authData){
authService.refreshToken()。然后(功能(响应){
//重定向到原来的请求 $ http.post(rejection.config)。然后(...,...); },
功能(错误){
$ location.path('/登录');
});
}
I implemented authInterceptorService so when response is not authorize I send request for new token and then I want to resend previoes request. So I need to store it somewhere and resend, what is best way to do it?
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);
}
解决方案
Why not make it its own request?
// define the original request
var originalRequest = function( some input ){
$http.post( 'url' , data ).then(onSuccess, _responseError);
}
then in your _responseError function, just call the original request
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);
}
Also, on failure, you do get the original configuration of the request, so you could just use that...
if (authData) {
authService.refreshToken().then(function (response) {
//redirect to original request
$http.post(rejection.config).then( ... , ... );
},
function (err) {
$location.path('/login');
});
}
这篇关于如何未被授权时,重发请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文