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.
Read locally. Nothing you paste leaves this tab.
What to do instead
The headers the API must send
With cookies or HTTP auth, the origin must be named exactly: enter the page's origin above.
Test the fix in your browser first
A HeaderDial profile with these as response headers, limited to the API host. HeaderDial needs site access to both the API host and the page's host.
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:
- Chrome and Edge:
Access to fetch at 'URL' from origin 'your origin' has been blocked by CORS policy: reason.fetchbecomesXMLHttpRequest,scriptorfontfor other request kinds.Response to preflight request doesn't pass access control check:before the reason means the OPTIONS preflight failed and the real request was never sent. - Firefox:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at URL. (Reason: reason). Status code: N.It does not name your origin. A failed preflight usually adds a second line,(Reason: CORS request did not succeed). Status code: (null).; the first line is the one that matters. The Network panel shows a short label such asCORS Missing Allow Origin. - Safari:
Origin your origin is not allowed by Access-Control-Allow-Origin. Status code: N, followed byFetch API cannot load URL due to access control checks.
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.
| Cause | Chrome / Edge says | Firefox says | Headers fix it? |
|---|---|---|---|
No Access-Control-Allow-Origin | No 'Access-Control-Allow-Origin' header is present on the requested resource. | CORS header ‘Access-Control-Allow-Origin’ missing | Yes |
| Preflight answer has no CORS headers | Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header… | same as above, then CORS request did not succeed | Yes: origin, methods and headers |
| Header not allowed | Request 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 response | Yes |
| Method not allowed | Method 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: 404 | No: the server must answer OPTIONS |
| Preflight redirected | Redirect 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 port | no CORS line, only net::ERR_CONNECTION_REFUSED | CORS request did not succeed). Status code: (null) alone | No: not a CORS problem |
| Page opened as a file | Cross 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
- The preflight gets an error status. A preflight needs a 2xx answer. A server with no
OPTIONSroute answers404or405; one whose auth middleware runs before CORS answers401, because the browser never sends yourAuthorizationheader with a preflight. Enable your framework's CORS middleware before auth, or answerOPTIONSwith204. In our tests in Chrome 159 and Firefox 158, adding every CORS header to a404preflight still failed (details), because a header rule cannot change a status code. - The preflight is redirected. Often
httptohttps, or/itemsto/items/. Call the URL the redirect points to. A redirect of the request itself is no better: after a redirect to another origin the browser sendsOrigin: null, so in our tests a target that named the page's origin failed in both browsers, and only*on both hops worked. - A file:// page. Serve the folder over http:
python3 -m http.server 8000ornpx serve, then openhttp://localhost:8000. - While developing, use a proxy. If you cannot change the API yet, let your dev server forward the calls. The browser then sees a same-origin request and sends no preflight. In Vite:
server.proxy: { '/api': { target: 'https://api.example.com', changeOrigin: true } }, and call/api/…from your code.
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