拦截器不被触发
Interceptors are not triggered
我正在尝试为我的应用实施全局授权处理。
在这里你可以看到我的代码:
.factory('Request', ['$resource', 'general',
function ($resource) {
return $resource(baseURL + ':resourceName/', {}, {
get : {
method : 'GET',
isArray : true,
transformResponse : function (data, headers) {
return JSON.parse(data).data;
},
interceptor: {
request : function (data) {
console.log("test");
},
requestError : function (data) {
console.log("test");
},
response : function (data) {
console.log("test");
},
responseError : function (data) {
console.log("test");
if(data.state == 401){
general.logIn({},false,function(){});
}
}
}
}
});
}
]);
然而,none 的拦截器被触发。代码有什么问题?
根据 Angular 文档,在 $resource 中使用的拦截器只有两个有效的拦截器对象方法:
interceptor - {Object=} - The interceptor object has two optional
methods - response and responseError. Both response and responseError
interceptors get called with http response object. See $http
interceptors.
参见:https://docs.angularjs.org/api/ngResource/service/$resource
如果您还需要请求方法,则必须遵循此处的文档:https://docs.angularjs.org/api/ng/service/$http
此外,我注意到您定义了两次 responseError,而不是 response 和 responseError:
responseError : function (data) {
console.log("test");
},
responseError : function (data) {
console.log("test");
if(data.state == 401){
general.logIn({},false,function(){});
}
}
摘自 Angular 有关 $resource 的文档:
interceptor - {Object=} - The interceptor object has two optional methods - response and responseError. Both response and responseError interceptors get called with http response object. See $http interceptors.
如果您想使用所有四个拦截器,我建议您使用 httpProvider
配置您的应用程序。您可以将所有 4 种可能性推送到 interceptors
数组。
像这样:
m.config(["$httpProvider", ($httpProvider: ng.IHttpProvider) => {
$httpProvider.interceptors.push(["$q" , (q: ng.IQService) => {
return {
'request': (config) => {
..Handle request...
return config;
},
'requestError': (rejection) => {
..Handle requestError...
return $q.reject(rejection);
},
'response': (response) => {
..Handle response...
return response;
},
'responseError': (rejection) => {
..Handle responseError...
return $q.reject(rejection);
}
}
}
]);
}]);
您错过了将 "general" 插入到服务函数中,因此
responseError : function (data) {
console.log("test");
if(data.state == 401){
general.logIn({},false,function(){});
}
}
根本不起作用
我正在尝试为我的应用实施全局授权处理。
在这里你可以看到我的代码:
.factory('Request', ['$resource', 'general',
function ($resource) {
return $resource(baseURL + ':resourceName/', {}, {
get : {
method : 'GET',
isArray : true,
transformResponse : function (data, headers) {
return JSON.parse(data).data;
},
interceptor: {
request : function (data) {
console.log("test");
},
requestError : function (data) {
console.log("test");
},
response : function (data) {
console.log("test");
},
responseError : function (data) {
console.log("test");
if(data.state == 401){
general.logIn({},false,function(){});
}
}
}
}
});
}
]);
然而,none 的拦截器被触发。代码有什么问题?
根据 Angular 文档,在 $resource 中使用的拦截器只有两个有效的拦截器对象方法:
interceptor - {Object=} - The interceptor object has two optional methods - response and responseError. Both response and responseError interceptors get called with http response object. See $http interceptors.
参见:https://docs.angularjs.org/api/ngResource/service/$resource
如果您还需要请求方法,则必须遵循此处的文档:https://docs.angularjs.org/api/ng/service/$http
此外,我注意到您定义了两次 responseError,而不是 response 和 responseError:
responseError : function (data) {
console.log("test");
},
responseError : function (data) {
console.log("test");
if(data.state == 401){
general.logIn({},false,function(){});
}
}
摘自 Angular 有关 $resource 的文档:
interceptor - {Object=} - The interceptor object has two optional methods - response and responseError. Both response and responseError interceptors get called with http response object. See $http interceptors.
如果您想使用所有四个拦截器,我建议您使用 httpProvider
配置您的应用程序。您可以将所有 4 种可能性推送到 interceptors
数组。
像这样:
m.config(["$httpProvider", ($httpProvider: ng.IHttpProvider) => {
$httpProvider.interceptors.push(["$q" , (q: ng.IQService) => {
return {
'request': (config) => {
..Handle request...
return config;
},
'requestError': (rejection) => {
..Handle requestError...
return $q.reject(rejection);
},
'response': (response) => {
..Handle response...
return response;
},
'responseError': (rejection) => {
..Handle responseError...
return $q.reject(rejection);
}
}
}
]);
}]);
您错过了将 "general" 插入到服务函数中,因此
responseError : function (data) {
console.log("test");
if(data.state == 401){
general.logIn({},false,function(){});
}
}
根本不起作用