加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 将’response’拒绝为’responseError’

发布时间:2020-12-17 07:36:52 所属栏目:安全 来源:网络整理
导读:有时,即使出现错误,我使用的API也会返回200.响应 JSON对象将如下所示: { error: true} 我已经构建了一个$http响应拦截器,它只是检查这个错误并拒绝它.我想要它然后跳转到我的responseError函数: $httpProvider.interceptors.push(function($q) { return {
有时,即使出现错误,我使用的API也会返回200.响应 JSON对象将如下所示:
{
    error: true
}

我已经构建了一个$http响应拦截器,它只是检查这个错误并拒绝它.我想要它然后跳转到我的responseError函数:

$httpProvider.interceptors.push(function($q) {
    return {

        response: function (response) {

            if (response.data.error) {

                // There has been an error,reject this response
                return $q.reject(response);
            }

            return response;
        },responseError: function(rejection) {

            // Display an error message to the user
            ...

            return $q.reject(rejection);
        }
    }
});

问题是,即使在拒绝响应之后,我的responseError函数也没有被调用.它被称为500错误等,所以我知道它的工作.我希望拒绝做同样的事情.

从docs:

responseError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.

关于什么缺失的任何想法?

看起来这是不可能做的.要减少重复的代码,只需单独声明错误处理函数,并在响应和responseError函数中重用它.
$httpProvider.interceptors.push(function($q) {

    var handleError = function (rejection) { ... }

    return {

        response: function (response) {

            if (response.data.error) {
                return handleError(response);
            }

            return response;
        },responseError: handleError
    }
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读