2

There are many similar questions around this topic but I could not find what I am missing in this fetch api request. could you someone help me to overcome this error ? how to set the http status ok in fetch api request. I understand we need to set this for OPTIONS method but not sure how to set this.

I have also tried adding no-cors mode but it is failing with code 415

The same request when I tried using Postman, it is working fine but failing fetch api request

Error: from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status

var myHeaders2 = new Headers();
myHeaders2.append("Content-Type", "application/json");
myHeaders2.append("Accept", "application/json");
myHeaders2.append('Acces-Control-Allow-Origin', 'https://example.com');
myHeaders2.append('Access-Control-Allow-Methods', "DELETE, POST, GET, OPTIONS");
myHeaders2.append("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, Origin, X-Auth-Token");
myHeaders2.append("Access-Control-Max-Age", "86400");
// myHeaders2.append("status", "200");


let obj = {};
obj.file_name = 'test1.txt';
obj.file_content = objJsonB64;

var raw = JSON.stringify(obj);

var uploadReqOptions = {
    method: 'POST',
    headers: myHeaders2,
    body: raw,
    redirect: 'follow'
};

fetch("https://destinationxyz.com", uploadReqOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log('error', error));

2 Answers2

-2

I have already face this issue. Told your api provider to set Acces-Control-Allow-Origin in their code. This not an issue of your code. Chilll.....

-2

Install chrome extention "CORS unblock". The problem is caused by your browser blocking the requests

Viktorlazi
  • 55
  • 11