【发布时间】:2019-07-22 05:00:31
【问题描述】:
我在使用 Angular 4 的 HttpClient get 方法时遇到了一个奇怪的问题。
我应该处理一些从后端返回的自定义错误消息并显示给用户。但是我在控制台中得到了 HttpErrorResponse
HttpErrorResponse:
error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0,
total: 0, type: "error", …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers:
Map(0)}
message: "Http failure response for (unknown url): 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: null
我读到了here 的问题,但没有一个解决方案对我有帮助。 在浏览器的网络选项卡中,我可以观察到来自服务器的真实响应:
"{\"StatusCode\":400,
\"ExceptionMessage\":\"Internal Server Error\",
\"Error\":\"Internal Server Error\",
\"Errors\":
[{\"Code\":\"ValidationError\",\"Message\":\"Custom message here.\"}]}"
请求在网络标签中看起来像这样:
General:
Request URL: https://asdfhost:port/5c77a77101ae8643aeeb61e9
Request Method: GET
Status Code: 400
Remote Address: address
Referrer Policy: no-referrer-when-downgrade
Response Headers:
cache-control: no-cache
content-type: application/json
date: Thu, 28 Feb 2019 09:42:22 GMT
expires: -1
pragma: no-cache
server: Kestrel
status: 400
Request Headers:
x-correlation-id: 6if123
Provisional headers are shown
Accept: application/json, text/plain, */*
Authorization: Bearer asdfasdftoken
Origin: http://origin
Referer: http://origin/somewhere
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36
自定义错误是否有可能破坏 xhr 请求? 任何帮助将不胜感激。 :)
【问题讨论】:
-
您是否添加了 HttpOptions 那么请也粘贴该代码。您需要检查 CORS 运行是否正常的另一个镜头。
标签: javascript angular error-handling xmlhttprequest httpclient