繁体   English   中英

Angular $ http在404上调用成功

[英]Angular $http calling success on 404

我通过服务公开这个功能:

function getData(url) {
    return $http.get(url).then(function (response) {
        return response.data;
    });
}

如果调用成功,一切都很好 - 我得到了一个按预期解决的承诺。

如果我传入生成404的URL,我会得到:

TypeError:无法读取未定义的属性“data”

在线: return response.data;

我可以在Chrome的开发者工具中看到GET返回404。

为什么Angular(v1.4.7, 也尝试使用v1.5.0 )调用我的successCallback并在错误上使用undefined

(我想要做的是处理调用代码中的失败。)

编辑: @jcaron指出我正确的方向。 问题似乎是此配置行(由另一个开发人员编写):

$httpProvider.interceptors.push('ErrorInterceptor');

拦截器必须有一个设计缺陷:

function ErrorInterceptor($q, $window) {
    return {
        response: function (response) {
            return response;
        },
        responseError: function (response) {
            if (response.status === 500) {
                $window.location.href = '/error';
                return $q.reject(response);
            }
        }
    };

拦截器必须有设计缺陷

确实有。 如果拒绝不是500状态,则返回undefined ,这符合承诺。 它应该是

…
responseError: function (err) {
    if (err.status === 500) {
        $window.location.href = '/error';
    }
    return $q.reject(err); // always pass on rejection
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM