I would like to make a simple http request in javascript with fetch()
, eg.:
async function fetchData() {
const res = await fetch("https://www.google.com");
console.log(res);
}
fetchData();
but this fails with:
Access to fetch at 'https://www.google.com/' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is 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.
There are lots of posts about this in internet and the most popular solutions revolve around setting up additional headers, I tried this as well, but it did not help:
const res = await fetch("https://www.google.com", {
headers: {
mode: "cors",
"Accept": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
}
// ...
This perfectly works with curl: curl "https://www.google.com"
.
Any ideas?