CORS Error Explained: What It Is and How to Fix It
If you work with frontend development, you’ve probably seen this error:
"Access to fetch at 'https://api.example.com' from origin 'http://localhost:3000' has been blocked by CORS policy"
CORS errors are confusing, frustrating, and extremely common. In this article, we’ll explain what a CORS error really is and how to fix it step by step.
What Is CORS?
CORS stands for Cross-Origin Resource Sharing.
By default, browsers block requests made from one origin to another for security reasons.
An origin is defined by:
- Protocol (http / https)
- Domain
- Port
If any of these differ, the browser considers it a different origin.
Why Browsers Block Cross-Origin Requests
This restriction exists to protect users from malicious websites.
Without CORS, a random website could:
- Read your private data
- Make authenticated requests on your behalf
- Steal sensitive information
The Most Common CORS Error
No 'Access-Control-Allow-Origin' header is present on the requested resource.
This means the server did not explicitly allow requests from your frontend origin.
Important: CORS Is a Browser Problem
This is a critical point many developers miss:
CORS is enforced by the browser, not by the server.
If you call the same API using:
- Postman
- curl
- A backend service
It usually works fine.
Preflight Requests Explained
Before sending certain requests, browsers send a OPTIONS request.
This is called a preflight request.
The server must respond with headers like:
Access-Control-Allow-Origin: http://localhost:3000 Access-Control-Allow-Methods: GET, POST, PUT Access-Control-Allow-Headers: Content-Type, Authorization
If the preflight fails, the real request is never sent.
How to Fix CORS Errors (Backend)
Fix 1: Allow Specific Origins
The safest solution is to allow known origins.
Access-Control-Allow-Origin: https://yourfrontend.com
Fix 2: Enable CORS in Common Backends
Node.js (Express)
const cors = require('cors');
app.use(cors({ origin: 'http://localhost:3000' }));
Python (Flask)
from flask_cors import CORS CORS(app, origins=["http://localhost:3000"])
ASP.NET Core
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend",
policy => policy.WithOrigins("http://localhost:3000")
.AllowAnyHeader()
.AllowAnyMethod());
});
How to Fix CORS Errors (Frontend)
Use a Proxy in Development
During development, proxies avoid CORS entirely.
Vite Example
server: {
proxy: {
'/api': 'http://localhost:5000'
}
}
Do NOT Use no-cors Mode
Using mode: 'no-cors' hides the error but makes the response unusable.
This is not a real fix.
Common CORS Mistakes
- Using
*with credentials - Fixing CORS on the frontend only
- Disabling CORS entirely in production
How to Debug CORS Errors
- Check the browser Network tab
- Inspect the preflight OPTIONS request
- Verify response headers
Conclusion
CORS errors are not bugs — they are security features.
Once you understand how origins, headers, and preflight requests work, CORS becomes predictable and manageable.
Most CORS issues are solved by configuring the backend correctly.

Comments
Post a Comment