我怎样才能在响应拦截器重新发送请求? [英] How can I send request again in response interceptor?
问题描述
我做了我的应用程序的拦截器,检测会话丢失(服务器发送一个HTTP 419)。在这种情况下,我需要从服务器请求一个新的会话,然后我想再次自动发送原始请求。结果
也许我可以保存在一个请求拦截的请求,然后重新发送,但也有可能是一个简单的解决方案。
请注意,我必须使用特定的Web服务创建会话。
angular.module('MyApp的',['ngResource'])。厂(
MyInterceptor',
功能($ Q $ rootScope){
复位功能(承诺){
返回promise.then(功能(响应){
//做事情成功
返回响应;
},函数(响应){
如果(response.status == 419){
//会话丢失
//创建新的会话的服务器端
// Session.query();
//然后重新发送当前请求
//?
}
返回$ q.reject(响应);
});
};
})。配置(函数($ httpProvider){
$ httpProvider.responseInterceptors.push('MyInterceptor');
});
下面是使用那些有兴趣的承诺我的解决方案。基本上你需要申请一个新的会话,并等待发送(使用response.config)对应的原始请求一个新的请求之前的响应。通过返回的承诺$ HTTP(response.config)可以确保响应将被视为好像它是原始请求。结果
(语法可能不是最好的,因为我是新来的承诺)
angular.module('MyApp的',['ngResource'])。厂(
MyInterceptor',
功能($ Q $ rootScope){
复位功能(承诺){
返回promise.then(功能(响应){
//做事情成功
返回响应;
},函数(响应){
如果(response.status == 419){
//会话丢失
VAR会话= $ injector.get(会议);
变量$ HTTP = $ injector.get('$ HTTP');
//首先创建新的会话的服务器端
变种延迟= $ q.defer();
VAR promiseSession = defer.promise;
Session.query({},函数(){
defer.resolve();
},函数(){
//错误
defer.reject();
});
//和连锁要求
VAR promiseUpdate = promiseSession.then(函数(){
返回$ HTTP(response.config);
});
返回promiseUpdate;
}
返回$ q.reject(响应);
});
};
})。配置(函数($ httpProvider){
$ httpProvider.responseInterceptors.push('MyInterceptor');
});
I've made an interceptor in my application that detects session loss (server sends an HTTP 419). In this case, I need to request a new session from the server, and then I would like to send the original request again automatically.
Maybe I could save the request in a request interceptor, and then send it again, but there might be a simpler solution.
Note that I have to use a specific webservice to create the session.
angular.module('myapp', [ 'ngResource' ]).factory(
'MyInterceptor',
function ($q, $rootScope) {
return function (promise) {
return promise.then(function (response) {
// do something on success
return response;
}, function (response) {
if(response.status == 419){
// session lost
// create new session server-side
// Session.query();
// then send current request again
// ???
}
return $q.reject(response);
});
};
}).config(function ($httpProvider) {
$httpProvider.responseInterceptors.push('MyInterceptor');
});
Here is my solution using promises for those interested. Basically you need to request a new session, and wait for the response before sending a new request corresponding to the original request (using response.config). By returning the promise $http(response.config) you ensure that the response will be treated as if it was the original request.
(syntax may not be the best as I'm new to promises)
angular.module('myapp', [ 'ngResource' ]).factory(
'MyInterceptor',
function ($q, $rootScope) {
return function (promise) {
return promise.then(function (response) {
// do something on success
return response;
}, function (response) {
if(response.status == 419){
// session lost
var Session = $injector.get('Session');
var $http = $injector.get('$http');
// first create new session server-side
var defer = $q.defer();
var promiseSession = defer.promise;
Session.query({},function(){
defer.resolve();
}, function(){
// error
defer.reject();
});
// and chain request
var promiseUpdate = promiseSession.then(function(){
return $http(response.config);
});
return promiseUpdate;
}
return $q.reject(response);
});
};
}).config(function ($httpProvider) {
$httpProvider.responseInterceptors.push('MyInterceptor');
});
这篇关于我怎样才能在响应拦截器重新发送请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!