问题描述
官方restangular文档这code样品重试在ErrorInterceptor请求
The official restangular documentation provides this code sample to retry a request in the ErrorInterceptor:
var refreshAccesstoken = function() {
var deferred = $q.defer();
// Refresh access-token logic
return deferred.promise;
};
Restangular.setErrorInterceptor(function(response, deferred, responseHandler) {
if(response.status === 403) {
refreshAccesstoken().then(function() {
// Repeat the request and then call the handlers the usual way.
$http(response.config).then(responseHandler, deferred.reject);
// Be aware that no request interceptors are called this way.
});
return false; // error handled
}
return true; // error not handled
});
然而,因为它在评论说,在第二次尝试将不会触发任何拦截器,因为它使用$ HTTP直接
However, as it says in the comments, the second attempt will not trigger any interceptors because it uses $http directly.
有没有一种方法,使第二个请求也经过Restangular管道并执行ErrorInterceptor?
Is there a way to make the second request also go through the Restangular pipeline and execute the ErrorInterceptor?
推荐答案
其实,请求制成的 Restangular 的已设计使用的承诺是一次解决。你不能重新发送被拒绝的情况下,
Actually, the requests made with Restangular use promises that have been designed to be resolved only once. You can't resend it in case of rejection.
我真的不知道,如果你想有一个可重复使用的 errorInterceptor 或没有,但服务工作的方式做不似乎允许重新构建的 Restangular对象的只是从响应对象配置
。
I don't really know if you want a re-usable errorInterceptor
or not, but the way the service works don't seems to allow to re-build a Restangular object simply from the response object config
.
有一件事情你可以做的是保存关于你的对象的,然后在使用它的拦截的
One thing you could do is save the reference to your object and then use it in the interceptor.
您应该有你的理由,但我必须与此提醒你,因为在失败的情况下,如果它仍然存在,你可以结束了一个无限通话,因为 Restangular 的总是会调用 errorInterceptor
。
You should have your reasons, but I must warn you with this because in case of failure and if it persists, you could end up with an infinite call, since the Restangular will always call the errorInterceptor
.
我嘲笑了一点为示范,只需打开网络选项卡,然后单击按钮,你会看到所有的请求进入低谷那里。
I've mocked up a little for the demonstration, just open your network tab and click the button, you should see all the request goes trough there.
这篇关于在拦截重试Restangular电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!