gpt4 book ai didi

Angularjs 1.3 $http 拦截器

转载 作者:行者123 更新时间:2023-12-04 15:22:25 24 4
gpt4 key购买 nike

我目前在我的应用程序中有以下内容,每当有 $http 请求运行时显示加载动画,然后隐藏在最后;

app.config(['$httpProvider', function ($httpProvider) {
var $http,
interceptor = ['$q', '$injector', function ($q, $injector) {
var error;

function response(response) {
// get $http via $injector because of circular dependency problem
$http = $http || $injector.get('$http');
if ($http.pendingRequests.length < 1) {
$('#loadingWidget').hide();
}
return response;
}

function responseError(response) {
if (response.status == 401) {
alert("You have been logged out or have tried to access a restricted area, redirecting to the login screen...");
window.location = globals.siteUrl + 'login';
} else {
// get $http via $injector because of circular dependency problem
$http = $http || $injector.get('$http');
if ($http.pendingRequests.length < 1) {
$('#loadingWidget').hide();
}
}
return $q.reject(response);
}

return function (promise) {
$('#loadingWidget').show();
return promise.then(response, responseError);
}
}];

$httpProvider.responseInterceptors.push(interceptor);

}]);

我一直在尝试将其转换为与 1.3 一起使用,但我似乎无法确定它。我一直在引用这些文档 $http (拦截器部分)但我不知道如何重写它。任何人都可以帮忙吗?

更新:这是我已经尝试过的:
app.factory('xmHttpInterceptor', function ($q, $http, $injector) {
return {
// optional method
'response': function (response) {
// get $http via $injector because of circular dependency problem
$http = $http || $injector.get('$http');
if ($http.pendingRequests.length < 1) {
$('#loadingWidget').hide();
}
return response;
},

// optional method
'responseError': function (rejection) {
alert(rejection);
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
});

和:
app.config(['$httpProvider', 'xmHttpInterceptor', function ($httpProvider, xmHttpInterceptor) {
$httpProvider.interceptors.push('xmHttpInterceptor');
}]);

但该网站无法加载:
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.0-beta.17/$injector/modulerr?p0=app&p1=Erro…s.org%2F1.3.0-beta.17%2F%24injector%2Funpr%3Fp0%3DxmHttpInterceptor%0A%20%...<omitted>...4) 

最佳答案

由于问题已经在评论中解决,我将其作为社区维基发布:

The $httpProvider.responseInterceptors is no longer support in 1.3, you have to use $httpProvider.interceptors instead.

-- runTarm



和:

Try not injecting your xmHttpInterceptor:

app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('xmHttpInterceptor');
}]);

-- David Bennett

关于Angularjs 1.3 $http 拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com