简体   繁体   English

获取状态代码 0 角度 HttpClient?

[英]Getting status code 0 angular HttpClient?

I have a strange problem using the angular 4's HttpClient get method.我在使用 angular 4 的 HttpClient get 方法时遇到了一个奇怪的问题。

There are some custom error messages returned from the backend which I should handle and display to the user.有一些从后端返回的自定义错误消息,我应该处理并显示给用户。 However I get HttpErrorResponse in the console但是我在控制台中得到了 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

I read about the problem here but none of the solutions helped me.我在这里读到了这个问题但没有一个解决方案对我有帮助。 In the network tab of the browser I'm able to observe the real response from the server:在浏览器的网络选项卡中,我能够观察到来自服务器的真实响应:

"{\"StatusCode\":400,
  \"ExceptionMessage\":\"Internal Server Error\",
  \"Error\":\"Internal Server Error\",
  \"Errors\": 
  [{\"Code\":\"ValidationError\",\"Message\":\"Custom message here.\"}]}"

The request seems like this in the network tab:该请求在网络选项卡中如下所示:

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

Is there a chance the custom errors to break the xhr request ?自定义错误是否有可能破坏 xhr 请求? Any help will be highly appreciated.任何帮助将不胜感激。 :) :)

Indeed it was a CORS problem.事实上,这是一个 CORS 问题。 When an error occurred the back-end didn't set the Allow Origin header.发生错误时,后端没有设置 Allow Origin 标头。

I had CORS enabled in my server and i was still getting the status 0, after digging the internet i find no solution then i tried some things and found out it was CORS issue however it was still returning status 0 because i had configured virtual host and virtual domain in hosts file and Angular Universal was calling actual domain not the loopback localhost domain.我在我的服务器中启用了 CORS 并且我仍然获得状态 0,在挖掘互联网之后我没有找到解决方案然后我尝试了一些事情并发现它是 CORS 问题但是它仍然返回状态 0 因为我已经配置了虚拟主机和主机文件中的虚拟域和 Angular Universal 正在调用实际域而不是环回本地主机域。 that is why it was getting invalid response from server.这就是为什么它从服务器收到无效响应的原因。

according to my research so you will get status 0 in following conditions根据我的研究,您将在以下条件下获得状态 0

1 - if Cross Origin Resource Sharing(CORS) headers are not set to * or requesting domain 1 - 如果跨源资源共享 (CORS) 标头未设置为 * 或请求域

2 - Sometimes CORS is enabled in normal request but not in PRE-FLIGHT request(options request) make sure you enable CORS on it as well. 2 - 有时在正常请求中启用了 CORS,但在飞行前请求(选项请求)中未启用,请确保您也在其上启用 CORS。

3 - You are using invalid domain or domains which are forwarding the requests to other domain . 3 - 您正在使用一个或多个无效域,这些域将请求转发到其他域。

This may have been a CORS error on the surface while the real problem is incorrect handling of CORS errors.这可能是表面上的 CORS 错误,而真正的问题是对 CORS 错误的不正确处理。 For example why is there no content-length response header.例如为什么没有content-length响应头。

According to the official documentation (as of Oct '21) HttpClient request errors have two types:根据官方文档(截至 21 年 10 月),HttpClient 请求错误有两种类型:

  • The server backend might reject the request, returning an HTTP response with a status code such as 404 or 500. These are error responses.服务器后端可能会拒绝请求,返回带有状态代码(例如 404 或 500)的 HTTP 响应。这些是错误响应。

  • Something could go wrong on the client-side such as a network error that prevents the request from completing successfully or an exception thrown in an RxJS operator.客户端可能会出错,例如阻止请求成功完成的网络错误或 RxJS 运算符中抛出的异常。 These errors have status set to 0 and the error property contains a ProgressEvent object, whose type might provide further information.这些错误的状态设置为 0,错误属性包含一个 ProgressEvent 对象,其类型可能提供更多信息。

Cross origin resource sharing errors are normal HTTP responses to the preflight requests made by your browser.跨源资源共享错误是对浏览器发出的预检请求的正常 HTTP 响应。 When the web server responds to a preflight request it may set the response status to any code.当 Web 服务器响应预检请求时,它可以将响应状态设置为任何代码。 Generally a 4xx.一般是 4xx。

Some tools may explicitly set the status to 0 if CORS fails, but this is not a hard rule.如果 CORS 失败,某些工具可能会明确将状态设置为 0,但这不是硬性规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM