“Access to fetch at ... has been blocked by CORS policy” is probably one of the most common errors encountered by anyone who has ever made a request from a frontend to another domain. Let’s look at what this mechanism is, why the browser blocks a request that works perfectly well through curl or Postman, and how to fix it on the server.
What CORS is and why it is needed
CORS (Cross-Origin Resource Sharing) is a browser security mechanism that controls whether a web page on one domain (origin) can make requests to resources on another domain. Without CORS, any website could send requests from a user’s browser to arbitrary APIs using the user’s authentication cookies—a classic attack vector.
An origin is defined by three things: protocol + domain + port. https://example.com and http://example.com are different origins. https://example.com and https://api.example.com are also different. Even https://example.com:3000 and https://example.com:8080 are different origins.
An important point that is often misunderstood: CORS is a browser restriction, not a server restriction. A server can always respond to a request from any origin—the browser decides, based on the response headers, whether to make the result available to the page’s JavaScript code. That is why curl and Postman “don’t see” the problem: they don’t enforce the CORS policy; this behavior is browser-specific.
Simple requests vs. preflight
Not every cross-origin request is handled in the same way.
A simple request is sent directly, without a preliminary check, if it meets these conditions: the method is GET, HEAD, or POST, and only “simple” headers are used (Accept, Accept-Language, and Content-Type with one of three basic values such as application/x-www-form-urlencoded).
A preflight request is used when a request does not meet the conditions for a simple request (for example, it uses the PUT/DELETE method or custom headers such as Authorization or Content-Type: application/json). The browser first sends a service request using the OPTIONS method to ask the server for permission, and only then sends the actual request.
OPTIONS /api/users HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: content-type, authorization
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: content-type, authorization
Access-Control-Max-Age: 86400If the server does not respond to the preflight with the correct headers, the browser will not send the actual request at all, and the familiar CORS policy error will appear in the console.
Key headers
- Access-Control-Allow-Origin — which origin can read the response. The value
*allows any origin, but only for requests without credentials (cookies, authorization headers). Specify the origin explicitly if cookies or authorization are needed. - Access-Control-Allow-Methods — which HTTP methods are allowed for cross-origin requests.
- Access-Control-Allow-Headers — which custom headers the client can send.
- Access-Control-Allow-Credentials: true — allows cookies and authorization headers to be sent. In this case,
Access-Control-Allow-Originmust contain a specific origin, not*—the browser will block the combination of credentials and a wildcard origin. - Access-Control-Max-Age — how many seconds the browser can cache the result of a preflight request, so it does not have to send OPTIONS before every request.
You can easily view the current headers on any site using the HTTP header analyzer. Sometimes the problem is not with the CORS logic, but with a proxy or CDN stripping the header before it reaches the client.
Common configuration errors
Wildcard origin combined with credentials
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: trueThis combination is not allowed by the specification—the browser will block the request even if the server sends it. The solution is to specify the origin explicitly (you can do this dynamically, using a list of allowed domains on the backend) rather than using a static asterisk.
CORS is configured for the main API but not for preflight
Sometimes middleware handles headers only for “actual” requests (GET/POST) and forgets to respond to OPTIONS. As a result, the preflight fails with 404 or 401 (because there is no route for OPTIONS or it requires authentication, which the browser does not yet send during the preflight).
Headers are added, but not to every response
A common case is middleware adding CORS headers to successful responses (200), but not to errors (4xx, 5xx). As a result, when the server returns an error, the console shows a CORS error that masks the real cause (for example, 500 on the backend), because the browser blocks access to the response body without the required headers.
Dynamic origin without validation
To support multiple domains (for example, app.example.com and admin.example.com), it is common to reflect the request’s Origin back in Access-Control-Allow-Origin without validation. This is effectively equivalent to a wildcard, but it also allows credentials—a serious security vulnerability if the list of allowed origins is not explicitly validated against an allowlist.
Configuration examples
Nginx
location /api/ {
set $cors_origin "";
if ($http_origin ~* "^https://(app|admin)\.example\.com$") {
set $cors_origin $http_origin;
}
add_header 'Access-Control-Allow-Origin' $cors_origin always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization' always;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 86400;
add_header 'Content-Length' 0;
return 204;
}
proxy_pass http://backend;
}Express (Node.js)
const cors = require('cors');
const allowedOrigins = ['https://app.example.com', 'https://admin.example.com'];
app.use(cors({
origin: (origin, callback) => {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
}));The cors library handles preflight requests itself—you do not need to write a separate route for OPTIONS.
How to diagnose the problem
- Open the Network tab in DevTools and find the request—actual or preflight (
OPTIONS). - Check the response status for the preflight. If it is 404/401/500, the problem is with routing or authorization, not CORS itself.
- Check the response headers: are
Access-Control-Allow-Origin,Access-Control-Allow-Methods, andAccess-Control-Allow-Headerspresent, and do they match what the client actually requests? You can run the requested URL through the CORS checker to quickly see which headers are actually returned. - Check whether a proxy or CDN in front of the server is stripping the CORS headers. Sometimes they are configured correctly on the backend but get lost at the infrastructure level.
Checklist
- Make sure the preflight (
OPTIONS) returns 2xx and the required headers - Do not combine
Access-Control-Allow-Origin: *withAccess-Control-Allow-Credentials: true - Validate origins against an allowlist if dynamic reflection is used
- Add CORS headers to all responses, including 4xx/5xx errors
- Check that headers are not being lost at the proxy/CDN level
- Use
Access-Control-Max-Ageto avoid sending unnecessary preflight requests with every request
Conclusion
CORS is not a bug or an obstacle to “work around,” but a deliberate browser security mechanism. Most problems come down to a few common causes: an unanswered preflight, an incorrect combination of a wildcard and credentials, or headers lost at a proxy. Check your current configuration with the CORS checker—the problem usually takes only a couple of minutes to find once you know which headers to look at.