I am trying to post some data on a backend server. but when i submit the data i got an error following below: Fetch at 'X' from origin 'localhost:8080' has been blocked by cors. No Access-Control-Allow origin in the header.
Though I have access-control-allow origin on header. The code is given below:
fetch(url, {
mode: "cors",
method: "POST",
headers: {
"Access-Control-Allow-Origin": "http://localhost:8080",
"Access-Control-Allow-Credentials": "true",
"Content-Type": "application/json",
"API-Key": "xxxxxxxxxxxxxx",
Accept: "application/json",
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
console.log("Success:", data);
})
.catch((error) => {
console.error("Error:", error);
});
I have tried no-cors policy but that server doesn't accept no-cors request.