【问题标题】:Angular check each resource response for error flag角度检查每个资源响应的错误标志
【发布时间】:2014-02-22 00:00:43
【问题描述】:

在 Angular 中,我希望有一个函数在 $resource 响应到达控制器之前拦截它,以查看它是否设置了错误标志,然后对该错误标志采取行动。在将响应数据发送到控制器之前,我是否可以使用资源函数来检查响应数据?

示例资源:

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;
});

【问题讨论】:

标签: angularjs angular-resource


【解决方案1】:

您可以全局定义资源的错误处理:

$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
     }
  }
});

响应参数为您提供了大量信息,以便您可以适当地处理错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多