[英]Calling http request from success of previous http response (avoiding callback hell) in Angular 4
[英]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.