[英]Angular $http calling success on 404
I'm exposing this function through a service: 我通过服务公开这个功能:
function getData(url) {
return $http.get(url).then(function (response) {
return response.data;
});
}
If the call succeeds, all is good - I get back a promise that resolves as expected. 如果调用成功,一切都很好 - 我得到了一个按预期解决的承诺。
If I pass in a URL that generates a 404, I get: 如果我传入生成404的URL,我会得到:
TypeError: Cannot read property 'data' of undefined
TypeError:无法读取未定义的属性“data”
on the line: return response.data;
在线:
return response.data;
I can see in Chrome's Developer Tools that the GET
returns 404. 我可以在Chrome的开发者工具中看到
GET
返回404。
Why is Angular (v1.4.7, also tried with v1.5.0 ) calling my successCallback with undefined
on an error? 为什么Angular(v1.4.7, 也尝试使用v1.5.0 )调用我的successCallback并在错误上使用
undefined
?
(What I want to do is handle failures in the calling code.) (我想要做的是处理调用代码中的失败。)
Edit: @jcaron pointed me in the right direction. 编辑: @jcaron指出我正确的方向。 The issue appears to be this configuration line (written by another developer):
问题似乎是此配置行(由另一个开发人员编写):
$httpProvider.interceptors.push('ErrorInterceptor');
The interceptor must have a design flaw: 拦截器必须有一个设计缺陷:
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);
}
}
};
The interceptor must have a design flaw
拦截器必须有设计缺陷
Indeed it has. 确实有。 It returns
undefined
in the case of a rejection that is not a 500 status, which fulfills the promise. 如果拒绝不是500状态,则返回
undefined
,这符合承诺。 It should be 它应该是
…
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.