Learning center

CORS error in the console: a missing header or a failed server?

A CORS error means the browser withheld a response from the page, not that CORS is misconfigured. A failing server often sends its error without CORS headers, so read the real status before changing CORS settings.

What the browser actually reported#

In this local reproduction, a page on http://localhost:8101 calls an API on http://127.0.0.1:8102. Different ports make different origins. The API returned 502 Bad Gateway with an HTML error page and no Access-Control-Allow-Origin header. Chromium printed:

Access to fetch at 'http://127.0.0.1:8102/api/fail-no-acao' from origin
'http://localhost:8101' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested resource.

Failed to load resource: net::ERR_FAILED

The page's fetch rejected with TypeError: Failed to fetch. The 502 appears nowhere the page can read it. The Network panel's Status column shows CORS error for such requests. Network panel Status values

When the same API sent the 502 with Access-Control-Allow-Origin: http://localhost:8101, the page received it normally: fetch resolved with status 502 and the Console printed Failed to load resource: the server responded with a status of 502 (Bad Gateway).

The browser behaves correctly in both cases. The Fetch standard only lets a page read a cross-origin response that passes the CORS check, and an error page without the header fails it. Fetch standard: CORS check ยท MDN: CORS errors

Two causes behind the same message#

Cause A: CORS is not configured for this origin or requestCause B: the server failed, and its error response lacks CORS headers
Does it ever work?Never from this origin, or never with this method or headerWorks most of the time, fails under load, during deploys or on specific inputs
Status outside the browser2xx without Access-Control-Allow-Origin, or with a value for another origin5xx, often from a proxy or load balancer, with an HTML body
Where the fix goesThe API's CORS configurationThe failing service, and its error responses' headers

The deciding observation is the status the server actually returned to the failing request. The page cannot read it, so read it outside the page: in the server or proxy log for that request, or in a replay made while the failure is happening.

Read the real status#

Replay with curl. In the Network panel, right-click the failed request and choose Copy > Copy as cURL. Run it in a terminal with -i added, so the status line and headers print. The copied command already carries the Origin header the browser sent. Copy as cURL

curl -i 'http://127.0.0.1:8102/api/fail-no-acao' -H 'Origin: http://localhost:8101'
HTTP/1.1 502 Bad Gateway
Content-Type: text/html

A replay shows what the server returns at the moment of the replay, not what it returned to the browser's request. Replay while the browser is still failing, and record both times. A 5xx then points at Cause B, and a 2xx without Access-Control-Allow-Origin points at Cause A. When the failure is intermittent, a later replay can return 200 after the original got a 503, so confirm the cause against the log entry for the original request. The curl replay guide covers copying cookies and tokens with care.

Check the server or proxy log for the original request's time and path. This is the record of the response the browser actually got. A request that never reached the application but appears in the load balancer's log with 502 is Cause B too.

Preflight failures read differently#

A request with a JSON Content-Type, a custom header or a method other than GET, HEAD or POST needs a preflight: the browser first sends an OPTIONS request and only sends the real request if the preflight passes. MDN: preflighted requests

In the reproduction, the preflight to /api/preflight-fails got a 404 without CORS headers. Chromium printed:

Access to fetch at 'http://127.0.0.1:8102/api/preflight-fails' from origin
'http://localhost:8101' has been blocked by CORS policy: Response to preflight
request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource.

The words Response to preflight request tell you the real request was never sent. Replay the OPTIONS request with curl, including Access-Control-Request-Method and Access-Control-Request-Headers, to see what the server returns for it. A route that has no OPTIONS handler, or an authentication layer that rejects the unauthenticated preflight, is a common reason.

Worked example: CORS errors only during deploys#

This example is fictional. A dashboard calls api.example.test from app.example.test. Users report occasional CORS errors on the Refresh button. CORS settings have not changed in months.

The Copy as cURL replay during the next deploy, while the dashboard showed the error, returned HTTP/1.1 503 Service Unavailable from the load balancer, with an HTML body and no CORS headers. Outside deploys, the same command returned 200 with Access-Control-Allow-Origin: https://app.example.test. The load balancer's log held a 503 for each reported failure's request, at the times users gave.

The report said: during deploys, the load balancer returns 503 without CORS headers, so the browser reports a CORS error. CORS for this origin is configured correctly. The fix went to the deploy's draining and health checks. A second change made the client show "Service temporarily unavailable" when fetch rejects, instead of leaving the button spinning.

What to put in the report#

  • The exact console message, including whether it mentions a preflight.
  • The page's origin and the request's URL and method.
  • The status and headers from a curl replay, with the time of the replay and of the original failure.
  • The log entry for the original request, if you can read it.
  • Whether the same request ever succeeds, and when it fails.
  • What you could not check, such as the server's own log.

Questions#

Should I add Access-Control-Allow-Origin: * to fix it?

Not before you know the real status. If the server failed, a wildcard only lets the page read the failure. A wildcard also cannot be used with credentialed requests. MDN: credentialed requests and wildcards

Why does the request work in curl or Postman?

CORS is enforced by browsers. Tools outside the browser do not apply it, which is what makes them useful for reading the status. A request that works in curl and fails in the page, with a 2xx and no CORS header in curl's output while the page is still failing, is a CORS configuration problem. If the page has stopped failing by the time you replay, the replay says nothing about the original response; read the log for it.

Does a CORS error mean the server did not process the request?

No. For a request without a preflight, the server receives and may act on it; the browser only withholds the response from the page. A POST that the browser reports as a CORS error may still have written data. Check the server's records before you retry.

Return to the method for browser bug reports for the other symptoms in this collection.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading