Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data using fetch API with mode 'no-cors'?

code looks like this, not sure how to read response data. Any idea?.

var url = 'http://www.bbc.co.uk/sport/football';

fetch(url, {
  mode : 'no-cors'
}).then(function(response) {
  console.log(response);
});

Response Object

like image 209
Anoop Mundathan Avatar asked Nov 05 '16 23:11

Anoop Mundathan


People also ask

What does no-CORS do on Fetch?

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.

How do I turn off CORS in fetch API?

How do you remove CORS? To get rid of a CORS error, you can download a browser extension like CORS Unblock ↗. The extension appends Access-Control-Allow-Origin: * to every HTTP response when it is enabled. It can also add custom Access-Control-Allow-Origin and Access-Control-Allow-Methods headers to the responses.

How do I retrieve data from a fetch request?

How do I return a response on Fetch? Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.


1 Answers

You can't.

If the origin doesn't support CORS, you can't actually get the response data directly. That's the whole point of no-cors... allowing you to use the response in certain ways, but not actually read/access the data.

like image 87
Brad Avatar answered Sep 28 '22 03:09

Brad