-2

I am using HttpClient to request the server I have no control of.
It is returning 200 HTTP status code if success and 400 is operation fail.

this.http
      .post(this.getUrl(), data, { headers: headers, responseType: 'text' })
      .pipe(catchError(this.handleError))
      .subscribe();

handleError(error) {
  error.status; // always CORS error (0) however real HTTP code is 200 or 400 
  ...  
}

For example that is the success code

enter image description here

So my question is there any way to get the real HTTP code instead of CORS error code using HttpClient if it is not possible is there at least a way to hide CORS warning messages in browser console?

sreginogemoh
  • 7,997
  • 18
  • 71
  • 125

2 Answers2

1

This is because HttpClient is triggering the pre-flight OPTION request (which it should) and due to CORS configuration on the server your api call failed.

If you don't have control over the api server, try check with the owner for how to allow your domain to call, and the allowed headers or methods in the request.

You can refer to this question, you might find a solution to fit your need.

Andy
  • 4,687
  • 2
  • 36
  • 43
1

One of the goals of browser sandboxing is to disallow a request from domain A, to make a get information from domain B. CORS is a way to explicitly allow access.

So if the appropriate CORS headers are not set, you being able to access information about the response (such as a status code) would be a major security issue.

The answer is therefore basically 'no', unless:

  1. You have control over the server and can change headers.
  2. You write a new service that proxies requests through a domain you do control.
Evert
  • 75,014
  • 17
  • 95
  • 156