HeaderDial › Guides

CORS error explainer: paste the console error, get the cause and the fix

Paste the CORS message from the browser console: has been blocked by CORS policy (Chrome and Edge), Cross-Origin Request Blocked (Firefox), or is not allowed by Access-Control-Allow-Origin (Safari). The page says what failed, whether your request reached the server, and whether response headers can fix it. Then it writes the headers the API must send, as plain headers or Express, nginx or Apache config, and a HeaderDial profile to try the fix in your own browser before you change the server. Everything runs in this page: it makes no network requests after it has loaded, and nothing you paste is sent anywhere.

Examples:

Read locally. Nothing you paste leaves this tab.

Your own APIs only. CORS protects users: it stops other sites from reading data with their browser and cookies. Add these headers to APIs you run, and send Access-Control-Allow-Origin with credentials only for origins you trust. The browser profile is for testing; it changes responses in your browser alone.

How to read a CORS error

The error your code catches says nothing useful on purpose: Chrome throws TypeError: Failed to fetch, Firefox TypeError: NetworkError when attempting to fetch resource., Safari TypeError: Load failed, and Axios AxiosError: Network Error. The reason is in a separate console line:

Every cause the explainer knows

Chrome and Firefox texts below were captured from Chrome 159 and Firefox 158 (three runs each, identical text; the full set is in the project's test fixtures); the Safari forms follow WebKit's message strings and were not captured in Safari.

CauseChrome / Edge saysFirefox saysHeaders fix it?
No Access-Control-Allow-OriginNo 'Access-Control-Allow-Origin' header is present on the requested resource.CORS header ‘Access-Control-Allow-Origin’ missingYes
Preflight answer has no CORS headersResponse to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header…same as above, then CORS request did not succeedYes: origin, methods and headers
Header not allowedRequest header field x-api-key is not allowed by Access-Control-Allow-Headers in preflight response.header ‘x-api-key’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight responseYes
Method not allowedMethod PUT is not allowed by Access-Control-Allow-Methods in preflight response.Did not find method in CORS header ‘Access-Control-Allow-Methods’Yes
* with cookies…must not be the wildcard '*' when the request's credentials mode is 'include'.Credential is not supported if the CORS header ‘Access-Control-Allow-Origin’ is ‘*’Yes: exact origin + Allow-Credentials: true
Credentials not allowed…'Access-Control-Allow-Credentials' header in the response is '' which must be 'true'…expected ‘true’ in CORS header ‘Access-Control-Allow-Credentials’Yes
Wrong origin allowed…has a value 'http://example.com' that is not equal to the supplied origin.CORS header ‘Access-Control-Allow-Origin’ does not match ‘…’Yes
Two values…contains multiple values '…', but only one is allowed.CORS header ‘Access-Control-Allow-Origin’ does not match ‘http://127.0.0.1:3000, http://example.com’ (the list, for a comma list or two header lines alike)Yes (a set row replaces both)
Preflight gets 404/405/401…It does not have HTTP ok status.CORS preflight response did not succeed). Status code: 404No: the server must answer OPTIONS
Preflight redirectedRedirect is not allowed for a preflight request.CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 301 (or 302, 307, 308)No: call the final URL
Server not running, wrong portno CORS line, only net::ERR_CONNECTION_REFUSEDCORS request did not succeed). Status code: (null) aloneNo: not a CORS problem
Page opened as a fileCross origin requests are only supported for protocol schemes: …CORS request not HTTP (Firefox 158 let a file:// page fetch a file in our test)No: serve it over http

When headers cannot fix it

Why mode: 'no-cors' is not the fix

Chrome's message ends with If an opaque response serves your needs, set the request's mode to 'no-cors'. With no-cors the request is sent, but your code gets an opaque response: status 0, no body, no headers, and it cannot send custom headers or a JSON Content-Type. It is for fire-and-forget requests and some images or scripts, not for reading an API.

Why test the fix in the browser first

CORS fixes often need two or three rounds: the origin first, then a header the preflight did not allow, then credentials. Each round on the server is a deploy. A HeaderDial profile adds the same response headers in your own browser, so you can paste each new error here, update the profile, and deploy once the request works. It needs site access to both the API host and the page's host, because the browser checks the response for the page that made the request. Give the profile an expiry (15 min to 1 day on each row) so it does not outlive the test. More on this in fix a CORS error while testing and CORS headers for local development.

How the page works

It matches the pasted text against the CORS messages Chrome, Edge, Firefox and Safari log in English, and reads the URL, the page origin (Chrome and Safari name it; Firefox does not) and any method or header name. The headers it suggests: Access-Control-Allow-Origin set to your page's origin (or * when the origin is unknown and no cookies are sent), Access-Control-Allow-Credentials: true when cookies are sent, and for a preflighted request Access-Control-Allow-Methods with the usual methods and Access-Control-Allow-Headers with Authorization, Content-Type and any header the error named. With a named origin the server should also send Vary: Origin, so caches keep one answer per origin. It is not in the browser profile, which only changes your own responses. Our end-to-end test feeds Chrome's real console errors into this page's code and applies the profile it builds until a credentialed PUT with a custom header works.

Add to ChromeFirefox: coming soon