【发布时间】:2014-09-08 13:53:07
【问题描述】:
restangular 官方文档provides 这个代码示例在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
});
但是,正如它在 cmets 中所说,第二次尝试不会触发任何拦截器,因为它直接使用 $http。
有没有办法让第二个请求也通过 Restangular 管道并执行 ErrorInterceptor?
【问题讨论】:
-
你可以用restApi调用替换
$http(response.config).then(responseHandler, deferred.reject);。使用$http拦截器而不是restangular拦截器,即使是restangular。我更喜欢 $http 拦截器,因为所有 angularjs api 包装器(如 restangular)在内部都使用 $http ... -
问题是关于在拦截器中从
$http切换到Restangular。不要将Restangular与简单的休息电话混淆。
标签: angularjs restangular