【发布时间】:2014-08-23 20:45:21
【问题描述】:
在 Angular js 中,我创建了一个服务,它应该对用户进行身份验证,它存储一个令牌并随每个请求一起发送,所以我使用了拦截器。 她是拦截器的代码。
app.factory('authInterceptorService',['$q', '$location', '$cookieStore', function( $q, $location, $cookieStore){
var authInterceptorServiceFactory = {};
var _request = function(config){
config.headers = config.headers || {};
var authData = $cookieStore.get('AuthorizationHeader');
if(authData){
config.headers.AuthorizationHeade = authData.token;
}
return config;
}
var _responseError = function(rejection) {
if(rejection.status === 401){
$location.path('/');
}
return $q.reject(rejection);
}
authInterceptorServiceFactory.request = _request;
authInterceptorServiceFactory.responseError = _responseError;
return authInterceptorServiceFactory;
}]);
问题是当我尝试添加拦截器时
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('authInterceptorService');
});
我得到 Uncaught TypeError: Cannot read property 'push' of undefined from AccApp
【问题讨论】:
标签: javascript angularjs angularjs-service angularjs-routing