【发布时间】:2017-02-06 12:21:35
【问题描述】:
我正在使用承诺从我的网络应用程序中查询服务。如果令牌无效,我现在想使用模式向用户询问凭据,从后端查询新令牌,重新运行失败的请求并返回我初始请求的成功块以继续,因为什么都没发生.
所以我设置了一个错误拦截器来捕获 401,并打开一个请求凭据的模式。然后我使用现有的 AuthService 查询一个令牌并返回 $http(response.config) 和调整后的令牌。
这是我最初的要求:
MyService.getData().then(
function(result) {
console.log("process data");
}
, function(response) {
console.log("error occurred retrieving data");
}
);
这是我的错误拦截器,对 401 做出反应:
Restangular.setErrorInterceptor(
function(response) {
if (response.status === 401) {
console.log("interceptor catches");
var modal = $uibModal.open({
templateUrl: "frame/view/Relogin.html",
controller: "ReloginController",
});
console.log("opened modal");
return modal.result.then(function(data) {
console.log("get new token");
return AuthService.authenticate(data[0], data[1]).then(
function (token) {
console.log("set new token");
},
function(){
console.log("failed getting new token");
})
.then(function() {
console.log("now rerun request");
return $http(response.config);
});
});
}
return $q.reject(response);
});
现在发生的是,拦截器捕获 401,并打开模式。但是现在不是等待用户输入并在后端查询新令牌,而是现在访问承诺的错误块。
所以控制台输出是:
interceptor catches
opened modal
error occurred retrieving data
get new token
set new token
now rerun request
但我希望它是:
interceptor catches
opened modal
get new token
set new token
now rerun request
process data
我认为使用 Promise 有一个错误...有人可以帮忙吗?
谢谢!
【问题讨论】:
标签: angularjs promise bootstrap-modal angular-promise