[英]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.