0

​Hello,

I have a problem with my HTTP requests. Is it possible please to help me understand what is happening?

When I am sending a GET request it is sent as OPTIONS:

let headers = new HttpHeaders();

headers = headers.set('Content-Type', 'application/json; charset=utf-8')
​.set('Authorization','bearer ' + this.auth.getUserToken());

 this.http.get(SERVER.URL_PROD + API.VERSION + API.GET_USERS, {headers: headers}).subscribe((res: any) => {
  console.log(res);
});

In my chrome dev tool debug I see the following:

Request Method: OPTIONS

Status Code: 405 Method Not Allowed
  • 1
    It's a CORS request https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – David Feb 05 '18 at 21:28
  • Possible duplicate of [Why is an OPTIONS request sent and can I disable it?](https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it) – Quentin Feb 05 '18 at 21:38

1 Answers1

5

Like David said, you are experiencing a CORS issue. Specifically when you are on a browser, the browser sends an OPTIONS request to the api/backend to say hey can I get this before firing a GET. If the response to OPTIONS comes back as no you cannot, such as your 405 then you are prevented from doing a GET.

In order to fix this, the backend that you are hitting needs to implement CORS and allow the various HTTP METHODS that it supports.

Mike Tung
  • 4,242
  • 1
  • 12
  • 20