Tru*_*ill 3 javascript ajax promise angularjs angular-promise
我通过服务公开这个功能:
function getData(url) {
return $http.get(url).then(function (response) {
return response.data;
});
}
Run Code Online (Sandbox Code Playgroud)
如果调用成功,一切都很好 - 我得到了一个按预期解决的承诺.
如果我传入生成404的URL,我会得到:
TypeError:无法读取未定义的属性"data"
在线上: return response.data;
我可以在Chrome的开发者工具中看到GET返回404.
为什么Angular(v1.4.7,也尝试使用v1.5.0)调用我的successCallback undefined出错了?
(我想要做的是处理调用代码中的失败.)
编辑: @jcaron指出我正确的方向.问题似乎是此配置行(由另一个开发人员编写):
$httpProvider.interceptors.push('ErrorInterceptor');
Run Code Online (Sandbox Code Playgroud)
拦截器必须有一个设计缺陷:
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);
}
}
};
Run Code Online (Sandbox Code Playgroud)
拦截器必须有设计缺陷
确实有.如果undefined拒绝不是500状态,则返回,这符合承诺.它应该是
…
responseError: function (err) {
if (err.status === 500) {
$window.location.href = '/error';
}
return $q.reject(err); // always pass on rejection
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
718 次 |
| 最近记录: |