我有一个Angular 6应用程序,当我发出http请求时:
this.myService.login(this.form.email.value, this.form.password.value)
.pipe(first())
.subscribe(
data =>
{
//do something on success
},
error =>
{
//here the response `error` if I use console.log(error)
//shows just Bad request if my API returns 404.
//How can I access other properties of the error, like
//the body?
}
);
如果HTTP调用返回错误,我如何访问错误的其他属性?
在出现错误的情况下,API将返回404 Bad Request,但也会有一个JSON主体:
{
Status: "Error",
Body : "Something happened"
}
如何在错误处理中访问此内容?
this.http.get(url, { observe: 'response' });
。您将获得响应状态以及所有详细信息。 - Sujata Chanda