Why does JavaScript give a CORS error, but the request works in Postman? Let's figure it out
Hello everyone! I'm a beginner frontend developer, and I've encountered a problem that has me stumped.
I'm writing a simple web application in vanilla JavaScript. I need to send a POST request to a third-party API (e.g., https://api.example.com/data) to get some data. In the browser console, I see an error:
Access to fetch at 'https://api.example.com/data' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Here's my code:
fetch('https://api.example.com/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ key: 'value' })
})
.then(response => response.json())
.then(data => console.log(d