🌐 “No ‘Access-Control-Allow-Origin'” is a Server Error
Browser blocks cross-origin requests for security. CORS headers on server allow specific origins. Fix is on server, not client.
📝 Server-Side Fixes
// Node.js (Express)
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', 'https://myapp.com');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', 'true');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
// .NET Core (Program.cs)
app.UseCors(policy => policy
.WithOrigins("https://myapp.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
// Nginx
add_header 'Access-Control-Allow-Origin' 'https://myapp.com';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,Content-Type';
🎯 Development Workarounds
// Browser extension: "CORS Unblock" (development only)
// Proxy in package.json (Create React App)
{
"proxy": "https://api.example.com"
}
// Vite proxy
export default {
server: {
proxy: {
'/api': 'https://api.example.com'
}
}
}
// Chrome flags (dev only, dangerous)
--disable-web-security --user-data-dir
💡 Important Notes
- Never use ‘*’ with credentials
- For development, allow localhost:3000, 3001, etc.
- Preflight (OPTIONS) requests must respond with 200
- CORS is browser-only (curl, Postman ignore CORS)
“Spent 3 hours debugging CORS. Fixed by adding Access-Control-Allow-Origin header on server. Always CORS is server issue, not client.”
