【发布时间】:2026-01-01 05:20:05
【问题描述】:
我正在尝试将错误从我的 API 传递到 Angular 4 应用程序。问题是在角度方面我看不到正确的错误消息:
Web Api 错误是这样生成的:
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Headers = {{"errorMessage", "Message in header"}},
Content = new StringContent("Message in body")
}
);
我的角度代码类似于:
return this._http.get<IMyObj[]>(this._serviceUrl)
.do(this.handleSuccessResponse)
.catch(this.handleErrorResponse);
protected handleErrorResponse(err: HttpErrorResponse) {
console.info(err);
// some logic based on code
}
问题是,如果我检查错误,它的“错误”属性为空,消息是“url 响应失败”并且没有自定义标头,但是在浏览器的网络选项卡上,我也可以看到我的自定义标头有错误作为身体的错误。如何从我的 Angular 应用程序访问这些消息?
【问题讨论】: