Home > OS >  Angular/Typescript: Interceptor makes the browser sent an additional OPTIONS http request
Angular/Typescript: Interceptor makes the browser sent an additional OPTIONS http request

Time:05-31

I am using an interceptor in order to add a JWT token to each request sent to the backend as described enter image description here

If I don't use the interceptor, the OPTIONS request does not get sent. Why is this happening, and can I keep the interceptor but get rid of the additional OPTIONS request?

CodePudding user response:

That's not the interceptor that's doing this. It's called CORS for Cross-Origin Resource Sharing.

Everybrowser is sending a OPTIONS request to domains that are different that the one hosting the page.

CodePudding user response:

Turns out as mentioned in this answer

As soon as you add a custom header, in my case Authorization or do anything that causes it to no longer be a simple request it, by default, sends the preflight request with the OPTIONS method instead of, in my case, POST.

It is also mentioned in more detail here.

So you can't avoid the additional request if you add custom headers to it.

  • Related