我使用Angular 2 Observable lib从服务器获取一些数据。问题是我想处理所有服务器状态(200,404,500),现在在控制台中显示错误。
return http.get( url, {headers})
.map(res => res.json())
.subscribe((res: any) => {
console.log(res);
}, (error: any) => {
console.log(error);
console.log('error finding variety list');
});
答案 0 :(得分:0)
要获取回复的状态,您可以这样做:
return http.get( url, {headers})
.map(res => {
// use res.status, here case if status is unauthorized
if(res.status === 401) {
throw new Error('Error unauthorized');
}
// case with no error
else {
return res.json();
}
})
.subscribe((res: any) =>
{
console.log(res);
}, (error: any) => {
console.log(error);
console.log('error finding variety list');
});