Security4 min read· 3 practice problems

No 'Access-Control-Allow-Origin' header CORS error: causes and fixes (preflight, cookies, error responses)

The browser console error has been blocked by CORS policy means the API response lacks the permission header. Imitate the preflight with curl to find the cause, then fix OPTIONS handling, cookies with an exact origin, and nginx always headers.

On this page
  1. Short answer: the response did not carry the permission header, so the browser blocked it
  2. What each console message means
  3. Diagnosis: imitate the preflight with curl
  4. Cause 2: the preflight is blocked by authentication
  5. Cause 3: cookies and the wildcard
  6. Cause 4: headers on error responses too — use always in nginx
  7. What not to do
  8. The order to work in
  9. Quick diagnostic checklist

Short answer: the response did not carry the permission header, so the browser blocked it

CORS is enforced by the browser. When a page calls another origin (a different scheme, host or port), the browser checks whether Access-Control-Allow-Origin in the response allows the page's origin; if not, it hides the response from JavaScript. That is why curl works but the browser fails. The fix belongs on the API server or the proxy in front of it. The cause is usually one of four:

  1. The server sends no CORS headers at all
  2. The preflight (OPTIONS) fails — blocked by auth, or an allowed header is missing
  3. Cookies are sent but the server allows *
  4. Error responses such as 401 or 502 carry no CORS headers — a different error looks like CORS

What each console message means

Access to fetch at 'https://api.example.com/v1/orders' from origin 'https://app.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  • With Response to preflight request doesn't pass access control check → the OPTIONS response is the problem (cause 2)
  • Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response → cause 2
  • must not be the wildcard '*' when the request's credentials mode is 'include' → cause 3

First look at the status code of the failed request in the DevTools Network tab. If it is 401, 403 or 502, it is cause 4.

Diagnosis: imitate the preflight with curl

curl -i -X OPTIONS https://api.example.com/v1/orders \
  -H "Origin: https://app.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type,authorization"
HTTP/2 204
access-control-allow-origin: https://app.example.com
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-headers: Content-Type, Authorization
access-control-allow-credentials: true
vary: Origin

This is healthy. If the access-control-* headers are missing or the status is 401, 404 or 405, that is your cause.

Cause 2: the preflight is blocked by authentication

Browsers never put cookies or Authorization on a preflight. If the auth layer checks OPTIONS too, it answers 401. Answer OPTIONS before authentication, and list the methods and headers the real request uses in Allow-Methods and Allow-Headers.

Cause 3: cookies and the wildcard

When cookies are sent with credentials: 'include', Access-Control-Allow-Origin: * is not allowed. Return the one exact origin together with Access-Control-Allow-Credentials: true and Vary: Origin. An origin with a trailing / does not match.

Cause 4: headers on error responses too — use always in nginx

Without always, nginx add_header applies only to normal responses such as 200, 204 and 3xx, so 401 and 502 responses lose the CORS headers and hide the real error. Add the headers in one place only, the app or nginx. Adding them in both gives a contains multiple values error.

# http block
map $http_origin $cors_origin {
    default                    "";
    "https://app.example.com"  $http_origin;
}
# location
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, OPTIONS" always;
add_header Access-Control-Allow-Headers     "Content-Type, Authorization" always;
add_header Vary Origin always;
if ($request_method = OPTIONS) { return 204; }

What not to do

  • Turning off browser security (--disable-web-security) or using mode: 'no-cors' — it works only on your machine, or you cannot read the response body.
  • Echoing back any request Origin unchecked while using cookies — any website can then read your API with the signed-in user's rights.

The order to work in

  1. In the Network tab, check the status code and whether a preflight was sent
  2. Run curl -i -X OPTIONS with Origin and Request-Method headers and read the response headers
  3. Answer OPTIONS with 204 before auth; add the allowed methods and headers
  4. With cookies, return the exact origin plus Allow-Credentials, and add headers to error responses (always)

To practise the browser-fails-but-curl-works case, try the one-minute quiz CORS error: No 'Access-Control-Allow-Origin' header is present.

Quick diagnostic checklist

  • In the Network tab, check the failed request's status code and whether a preflight (OPTIONS) was sent.
  • Run curl -i -X OPTIONS with Origin and Access-Control-Request-Method headers and read the response headers.
  • A 401 on the preflight means the auth layer is blocking OPTIONS.
  • Check that every method and header the real request uses is in Allow-Methods and Allow-Headers.
  • With cookies, send the exact origin and Access-Control-Allow-Credentials: true instead of *.
  • When the returned origin varies, also send Vary: Origin.
  • Add always to nginx add_header so 401 and 502 responses carry the headers too.
  • Set CORS headers in one place only, the app or the proxy, to avoid duplicates.

Practice with this guide

Reviewed problems about the same failure. Write the cause, recovery, and prevention yourself, then compare with the model answer.

Read next