0

I have such a generic method to send post requests

async function postData(uri = '', data = {}) {
  let url = process.env.VUE_APP_API_URL + uri;
  const response = await fetch(url, {
    method: 'POST',
    mode: 'cors',
    cache: 'no-cache',
    credentials: 'omit',
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  });

  return await handleErrors(response)
}

function handleErrors(response) {
  if (!response.ok) {
    throw Error(response.statusText);
  }
  return response;
}

as you can see I set Access-Control-Allow-Origin to * , which means all URLs allowed, right? but for some reason I still see cors issue when sending POST requests, but Access-Control-Allow-Origin header exists in the request headers https://gyazo.com/f59fb77cc812f76952f7fbc243356415.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Bogdan Dubyk
  • 4,756
  • 7
  • 30
  • 67

1 Answers1

1

Access-Control-Allow-Origin should be in response headers not request headers.

You need to set the header server side.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65