Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making fetch API work with CORS after OPTIONS response

Tags:

I are trying to fetch data from our API. The API has enabled CORS support and returns the below response to the OPTIONS request:

Access-Control-Request-Headers:content-type   Access-Control-Allow-Origin:*   

The API doesn't allow 'Content-type' anything other than 'application/json'.

Using this limitation, I am trying to use the fetch method of React-Native to get the data.

Method 1 (no-cors):

{     method: 'POST',     mode: "no-cors",     headers: {        'content-type': 'application/json' } 

With this method, the browser automatically sends the content-type as 'text/plain'. I assume this is because CORS allow just one of the three headers by default. However, since the server doesn't support this content-type, it returns an error back for unsupported content type.

Method 2 (with cors or with nothing):

{      method: 'POST',     mode: "cors", // or without this line     redirect: 'follow',     headers: {         'content-type': 'application/json'     } }    ...    .then(response => console.log(response)) 

In this scenario, using Chrome's F12 network tool, I can see the server returning data : the first request to the server is a fetch for OPTIONS. To this, the server replies back with an empty object along with the above headers set. The next call is the actual POST API call, to which the server responds back with a proper JSON response containing some data. However, the response which is getting on the console via my code is {}. I assume this is because the react's fetch API is returning back the response of the OPTIONS call instead of the actual POST call.

Is there any way to ignore the response of the OPTIONS request and get the then method to process the response of the subsequent request?

like image 741
letsc Avatar asked May 03 '17 12:05

letsc


People also ask

How do I use fetch the resource with CORS disabled?

CORS Should Always Be Handled From Server Side! present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. It states that there's a missing Access-Control-Allow-Origin header on the resource you requested.

What does fetch no-CORS do?

no-cors. Prevents the method from being anything other than HEAD , GET or POST , and the headers from being anything other than simple headers. If any ServiceWorkers intercept these requests, they may not add or override any headers except for those that are simple headers.


1 Answers

The immediate problem you’re hitting is, your code as currently written expects the response to be JSON but the response is actually a Promise that you need to handle to get the JSON.

So you need to instead do something like this:

fetch("https://example.com")     .then(response => response.json())     .then(jsondata => console.log(jsondata)) 
like image 147
sideshowbarker Avatar answered Sep 21 '22 08:09

sideshowbarker