Fix: CORS Policy Error (Access-Control-Allow-Origin)
Exact message
Access to fetch at 'http://...' from origin 'http://...' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present.
Quick answer
CORS is a browser security feature. This error means your backend server is not sending the 'Access-Control-Allow-Origin' header, so the browser blocks your frontend from reading the response.
🛠️ Fix it right here with AI Code Explainer
Edit the traceback below if needed, then let our AI suggest a fix without leaving this page.
Why this error happens
Browsers enforce the Same-Origin Policy. If your frontend calls an API on a different domain, the server must explicitly allow it via CORS headers.
Example: Wrong vs Right
Wrong
// Frontend fetch request
fetch('http://localhost:5000/api/data')
.then(res => res.json());
// Browser blocks it if backend doesn't have CORS headersRight
// Backend (Express.js example)
const cors = require('cors');
app.use(cors({ origin: 'http://localhost:5173' }));How to fix it (step by step)
- Understand that CORS is a server-side fix, not a frontend fix
- If you control the backend, add a CORS middleware
- Configure the specific origin for security
- For development only, you can use a proxy in Vite/Webpack
Frequently asked questions
Why does it work in Postman but not the browser?
Postman is a backend tool and does not enforce browser security policies like CORS.
What is a preflight request?
For non-simple requests, the browser sends an OPTIONS request first. The server must respond with the allowed methods and headers.
Related Search Terms
Related JS errors
Need to fix JS code not just errors?
Use the free to paste any JS snippet and get a line-by-line explanation.
Want an AI with memory for your project?
MonsterX AI remembers your codebase across chats.