Skip to content
Advertisement

How to resolve CORS error in Fetch API js

I want to fetch json from my subdomain (File Hosting Server) , but it gets the following error:

Reason: CORS request did not succeed)

(Reason: CORS header ‘Access-Control-Allow-Origin’ missing

my similar code:

async function getData(url = '', data = {}) {
    const response = await fetch(url, {
    method: 'get',
    mode: 'no-cors',
    headers: {
       'Access-Control-Allow-Origin' : '*'
    },
  });
  return response.json();
}

I add

headers: {
}                               'Access-Control-Allow-Origin' : '*'

but don’t work.

I add

mode: no-cors

to code, fetch file but return response status 0 , mode: ‘opaque’.

please help me.

Advertisement

Answer

CORS is configured through server-side, so you need to configure on your File Hosting Server

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement