const runFunction = async () => {
try {
const getAPI = await fetch(`https://xkcd.com/1/info.0.json`);
const resp = await getAPI.json();
console.log(resp);
} catch (error) {
console.log(error);
}
}
runFunction();
I'm trying to get information from an API using fetch and async/await but I am getting a CORS error. I have tried adding multiple headers such as:
const getAPI = await fetch(`https://xkcd.com/1/info.0.json`,
{
headers:
{
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Methods": "HEAD, GET, POST, PUT, PATCH, DELETE",
"Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token"
}
});
Doing that still gives me the error "No 'Access-Control-Allow-Origin' header is present on the requested resource". The only way I've been able to solve the problem is by adding a CORS unblock extension to my browser. I don't think thats an efficient way of going about my code because anyone accessing my project will have to download the extension.
To fix this problem, will I have to create a node app? Is there no way to fix this on the client side?