has been blocked by CORS policy
JavaScript Programming Language
Severity: ModerateWhat it means
Chrome and Edge: Access to fetch at 'https://api.example.com/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Firefox: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ..., with the reason in brackets.
MDN: most CORS errors can only be fixed on the server, because the server decides whether other sites may read its responses. That's why the same request works in Postman or curl, which don't enforce CORS.
Affected Models
- Web browsers (Chrome, Edge, Firefox, Safari)
How to Fix It
-
Read the part after the colon. It says what was missing: no Access-Control-Allow-Origin header, a preflight request that failed, or a request header the server doesn't allow.
MDN: your JavaScript code isn't told the details, only that it failed. The console is the only place that shows them.
-
Your server? Make it send Access-Control-Allow-Origin with your page's origin, for example Access-Control-Allow-Origin: https://example.com.
MDN: the * wildcard allows any site, and is only for public APIs. It also stops cookies and other credentials from being sent.
-
Preflight failed (Response to preflight request doesn't pass access control check)? The server must also answer the OPTIONS request the browser sends first. Or keep the request simple, so the browser skips it: GET, HEAD or POST, safelisted headers only, and a Content-Type of text/plain, multipart/form-data or application/x-www-form-urlencoded.
-
Not your server, and it sends no CORS headers? Route the request through a server you control, which fetches it and answers with the right headers. MDN lists this option.
mode: "no-cors" in fetch() stops the error, but the response comes back empty and unreadable. MDN: use it only when you don't need to read the response.