[英]Angularjs $http promise; Get the callback functions fn1 and fn2 from $http().then(fn1, fn2)
我正在做一个自定义的Oauth2身份验证模块。 我有一个requestExecutor工厂:
$http({
url: requestObject.url,
method: requestObject.method,
cache: requestObject.cache,
headers: requestObject.headers,
data: requestObject.data})
.then(function (resData) {
if (requestObject.callback) {
requestObject.callback(resData.data);
}
}, function () {
if (requestObject && requestObject.customErrorCallback) {
requestObject.customErrorCallback();
}
});
和Http拦截器:
'responseError': function (rejection) {
console.log(rejection)
switch (rejection.status) {
case 401 :
{
$rootScope.$emit(CONST.UNAUTHORIZED);
break;
}
case 403 :
{
$rootScope.$emit(CONST.FORBIDDEN, rejection.config);
break;
}
}
return $q.reject(rejection);
}
因此,当我执行一个请求并从服务器获取403响应错误时,我想将完整的请求发送给CONST.FORBIDDEN
的侦听CONST.FORBIDDEN
(唯一缺少的是$http().then()
的回调) 。 原因是在完成访问令牌的刷新后,我想再次执行失败的请求。
我的问题是:
$http().then()
在哪里存储? $http().then()
承诺吗? 不确定您的意图,可能会有更好的解决方案,但是您可以在config对象中注册回调处理程序,并在拦截器中访问它们。 plnkr
但是这是一个更好的设计。 angular-app / securityInterceptor您将在拦截器中刷新并再次执行请求。 如果第二个请求成功,则返回结果,成功/错误处理程序将处理该结果。
$scope.fetchAsync = function(forceError) {
function successHandler(result) {
$scope.content = result.data;
}
function errorHandler(result) {
$scope.content = result;
}
$scope.content = 'Loading...';
$http.get(forceError ? 'not-found' : 'test.txt', {
handler: {
success: successHandler,
error: errorHandler
}
}).then(successHandler, errorHandler);
}
$httpProvider.interceptors.push(function($q) {
return {
'responseError': function(rejection) {
console.log(rejection.config.handler);
return rejection;
}
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.