Angularjs 角度检查每个资源响应的错误标志
在Angular中,我希望有一个函数在$resource响应到达控制器之前拦截它,以查看它是否设置了错误标志,然后对该错误标志执行操作。在响应数据发送到控制器之前,我是否可以挂接一个资源函数来检查响应数据 示例资源:Angularjs 角度检查每个资源响应的错误标志,angularjs,angular-resource,Angularjs,Angular Resource,在Angular中,我希望有一个函数在$resource响应到达控制器之前拦截它,以查看它是否设置了错误标志,然后对该错误标志执行操作。在响应数据发送到控制器之前,我是否可以挂接一个资源函数来检查响应数据 示例资源: mymod.factory('setSomething', function($resource){ var resource = $resource('/proxy/request.php?action=setSomething', {}, { post:
mymod.factory('setSomething', function($resource){
var resource = $resource('/proxy/request.php?action=setSomething', {}, {
post:{
method : "POST",
isArray : false,
headers : {
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'
}
},
});
return resource;
});
您可以全局定义资源的错误处理:
$httpProvider.interceptors.push(function($q, $rootScope) {
return {
'request': function(config) {
// intercepts the request
},
'response': function(response) {
// intercepts the response. you can examine things like status codes
},
'responseError': function(response) {
// intercepts the response when the response was an error
}
}
});
response参数为您提供了大量可处理的信息,因此您可以适当地处理错误。检查此其他答案,应该给出您所需要的答案。您可能可以使用$http拦截器。有一篇关于拦截器的好帖子。