![](/img/trans.png)
[英]How to throw observable error in .map for a http status 200 response in Angular 4
[英]How to check http 200 status code in angular if its not available in the json response
驗證服務
logout() {
return this.http.post(this.logOutApi, null);
}
狀態代碼不會在后端的json響應中顯示,但會顯示在郵遞員的狀態上。 如何獲取狀態碼。
ts文件
logout() {
this.chk.logout().subscribe((res: any)=>{
if(res.status == 200) //doesnt work{
console.log(res);
})
}
}, (err)=>{
alert("There was a problem logging you out");
});
}
error
回調
, (err)=>{
alert(err.status);
});
您可以使用{ observe: 'response' }
來讀取完整的響應,包括成功處理程序中的狀態代碼。 這將使您可以訪問HttpResponse類型的響應:
服務:
logout() {
// you should consider providing a type
return this.http.post(this.logOutApi, null, { observe: 'response' });
}
零件:
logout() {
this.chk.logout().subscribe(
(res) => {
if (res.status == 200) {
console.log(res);
})
}
}, (err) => {
alert("There was a problem logging you out");
});
}
希望有幫助!
除200以外,您將在錯誤塊中獲得狀態代碼。 所以在這里您將必須像下面這樣處理
logout() {
this.chk.logout().subscribe((res: any)=>{
if(res.status == 200) //doesnt work{
console.log(res);
})
}
}, (error)=>{
if (error.status === 500) {
alert('Server down please try after some time');
}
else if (error.status === 404) {
alert('Server down. Please try after some time');
}
});
}
希望這個幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.