When JavaScript on a website fails to fetch data from another API, the browser console sometimes displays a message like “blocked by CORS policy.” This error is often mistaken for a JavaScript or internet connection issue, but the cause usually relates to permissions sent by the server. Understanding CORS helps developers identify the source of the problem without opening up API access indiscriminately.
What Is CORS?
Cross-Origin Resource Sharing (CORS) is a mechanism based on HTTP headers that allows a server to declare which origins are permitted to read its response through the browser. An origin consists of the scheme or protocol, hostname, and port. Therefore, https://app.example.com and http://app.example.com are considered different, as are two addresses with different ports.
The browser enforces the same-origin policy as a security boundary. Generally, scripts from one origin cannot read responses from another origin unless the target server provides appropriate CORS permissions. CORS is not a login system and not a substitute for authentication. This mechanism primarily controls whether code running in the browser is allowed to read cross-origin responses.
How CORS Works
When an application uses fetch() or XMLHttpRequest to request data from another origin, the browser includes an Origin header. The server can then respond with headers such as Access-Control-Allow-Origin. If the requesting origin is permitted and other requirements are met, the browser provides the response to JavaScript.
For certain requests, the browser sends a preflight request first using the OPTIONS method. This commonly occurs when the request uses specific methods, custom headers, or content types that fall outside the categories simplified by the standard. The preflight gives the server an opportunity to declare which methods and headers are allowed before the browser sends the main request.
Some commonly encountered headers:
Access-Control-Allow-Origin: the origin permitted to read the response.Access-Control-Allow-Methods: the HTTP methods allowed for cross-origin requests.Access-Control-Allow-Headers: the request headers that may be used.Access-Control-Allow-Credentials: indicates whether the response can be shared when the request includes credentials.Access-Control-Max-Age: the duration for which the browser may cache the preflight result, subject to browser-imposed maximum limits.
Common Causes of CORS Errors
1. Origin Mismatch
The server may allow https://app.example.com, while the application runs on https://www.example.com, a local address, or a different port. Match the origin value visible in the Network tab with the value permitted in the server configuration. Protocol, host, and port all matter.
2. Preflight Not Handled Correctly
If the browser sends OPTIONS but the server, reverse proxy, or firewall rejects that request, the main request may fail before being sent. Ensure that the endpoint and relevant server layers can respond to preflight with appropriate method and header headers. Do not assume all requests will trigger a preflight; check which requests actually appear in the browser.
3. Headers or Methods Not Allowed
For example, the application sends an Authorization header or custom headers, but the preflight response does not list them in Access-Control-Allow-Headers. Also check whether the method being used, such as PUT or DELETE, is included in Access-Control-Allow-Methods when the preflight requests it.
4. Credentials Combined with Wildcard
For requests that include cookies or other credentials, the server must not use Access-Control-Allow-Origin: *. The server must return the specific origin and set Access-Control-Allow-Credentials: true if required. Enable credentials only for trusted origins and according to application needs.
5. Response Redirected or Headers Missing
Redirects to login, CDN, or another endpoint can cause CORS flow to fail, especially for requests requiring preflight. CORS headers may also be missing on error responses because they are configured only for successful responses. Check the actual response from the endpoint, including 401, 403, and 5xx statuses, as well as rules in the application server, proxy, and CDN.
How to Inspect and Fix CORS Errors
- Open the browser’s Developer Tools, then select the Console tab to read the complete error message.
- Open the Network tab, repeat the request, and select the failed API request. Check the URL, method, status,
Originheader, and CORS headers in the response. - If there is an
OPTIONSrequest, check whether the status and response headers match the method and headers requested by the browser. - Fix the configuration on the server or API gateway. Use a list of origins that are actually needed, allow only necessary methods and headers, and ensure rules apply to error responses as well.
- Retest from the actual application origin. Requests via curl or server-side clients can help check headers but do not fully replicate browser security decisions.
Do not attempt to resolve the issue by disabling browser security. The no-cors mode in the Fetch API is also not a shortcut to read API data: the response becomes opaque, so JavaScript cannot read the body and response headers as usual.
Safer CORS Practices
- Allow only origins that genuinely need access; avoid automatically echoing the
Originvalue without validation. - Do not enable cross-origin credentials for all sites. Combine cookies with appropriate authentication and CSRF protection.
- Limit methods and headers to application needs rather than opening everything just to make errors disappear.
- If the response varies based on the requested origin, consider the
Vary: Originheader so intermediate caches can distinguish response variations correctly. - If a third-party API does not permit your website’s origin, use your own backend only if authorized and necessary. Protect that proxy with authentication, input validation, rate limiting, and an allowlist of destinations to prevent it from becoming an open proxy.
Conclusion
CORS is a cross-origin response sharing rule enforced by the browser, while the primary permissions are determined by the target server. When an error occurs, start with Console and Network, match the origin, then check the preflight response and CORS headers on the failing endpoint. The correct fix is to set the narrowest possible permissions on the server—not disabling browser protections or using wildcards without understanding the implications.
References: CORS Guide on MDN Web Docs and CORS Errors troubleshooting guide on MDN.

Komentar (0)
Belum ada komentar. Jadilah yang pertama berkomentar.