在服务中,有以下代码:
getUser(id){
return this.http.get('http:..../' + id)
.map(res => res.json());
}
在组件中:
this.myService.getUser(this.id).subscribe((customer) => {
console.log(customer);
this.customer = customer,
(err) => console.log(err)
});
当它是“客户”存在时,没问题我可以获得有关客户的所有信息。
当 id 不存在时,web api 会返回带有消息的“BadRequest”。我怎样才能得到这个消息?状态?
谢谢,
原文由 Kris-I 发布,翻译遵循 CC BY-SA 4.0 许可协议
(err)
需要在customer
胖箭头之外:要返回错误消息,请添加
catch
这将返回错误对象: