How to Fix CORS Errors

Cross-origin resource sharing (CORS) lets a server specify which pages on other origins may read its responses in a browser. A CORS error can mean that the server's response headers do not permit the request, but some CORS messages also indicate a failed network connection. Identify which request failed and why before changing the configuration. Fixing the connection or the server's response for the intended origin addresses the cause; disabling browser security only hides the problem.
An origin consists of a scheme, a host, and a port. https://app.example and https://api.example are different origins, as are HTTP and HTTPS versions of one host. Before editing headers, note the page origin, final API URL, request method, custom request headers, and the request's credential mode. Use those details to find the specific mismatch rather than allowing every origin or operation.
Read the Browser Error and Find the Failed Request
Open the browser developer tools for the page you are testing, reproduce the request, and read the complete console message. In the Network panel, filter for the target URL. Look for an OPTIONS request, which checks permission before the intended GET, POST, or other request. This preliminary exchange is called a preflight; the browser may reuse a cached result, so a missing OPTIONS entry does not prove that preflight is unnecessary.
If there is no HTTP response, check for a DNS lookup failure, a refused connection, a timeout, or an invalid TLS certificate. An HTTPS page requesting an HTTP API can also encounter a mixed-content block. Correct the connection or URL problem first; adding CORS headers cannot repair a connection that fails before an HTTP response is received.
When a response is available, record its status and headers, including any redirects or errors. A successful application status does not guarantee that JavaScript can read the response. A server, proxy, or authentication failure may also prevent the expected CORS headers from being returned, so identify which component produced the failing response.
Inspect the Origin and Preflight Exchange
The Origin request header identifies the requesting origin. For a preflight, the browser sends OPTIONS with Access-Control-Request-Method and, when needed, Access-Control-Request-Headers. Compare those values with the response's Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers. The preflight response needs a successful 2xx status and the permissions required for that origin, method, and set of request headers.
For a cross-origin script request, a method or script-set header outside the CORS safelist triggers a preflight. Examples include a POST with Content-Type: application/json or a request carrying an Authorization header. Check the headers as well as the method; GET does not automatically mean that preflight can be skipped.
The preflight does not carry the cookies or HTTP authentication credentials intended for the actual request. If a gateway demands a login before answering OPTIONS, configure it to handle the preflight without that login challenge. The actual request still needs the API's normal authentication and authorization checks.
For example, suppose an approved page at https://app.example sends a JSON POST using the default fetch credential setting and no additional custom request headers. A preflight response can use status 204 with Access-Control-Allow-Origin: https://app.example, Access-Control-Allow-Methods: POST, and Access-Control-Allow-Headers: Content-Type. The actual POST response also needs the matching Access-Control-Allow-Origin header.
Correct the Server Response
Configure CORS in the application, reverse proxy, gateway, or content delivery network (CDN) that sends the response to the browser. Apply the policy to relevant error responses as well as successful ones. An actual response with status 401, 404, or 500 can still be readable by JavaScript when it has the required CORS headers; without those headers, the underlying error can look like a CORS failure. Inspect the response that actually reached the browser, since an intermediary can remove, replace, or cache headers.
Allow only the origins that need browser access. For public data, when the request's credential mode is not include, Access-Control-Allow-Origin: * can be appropriate. For an origin-specific policy, return the exact permitted origin rather than an informal list of domains in one header value. If the origin is not approved, do not reflect it merely because it appeared in the request. Likewise, list methods and request headers that the API intentionally supports rather than indiscriminately allowing every operation.
CORS controls browser access to a response; it does not authenticate callers or protect an API from direct HTTP clients. Keep normal authentication, authorization, input validation, and abuse controls in place. A passing CORS check means the browser may expose the response to that origin under the protocol rules, not that the underlying request is safe or the caller is entitled to data.
Handle Credentials and Shared Caches
When a fetch request uses credentials: "include", the response must specify the permitted origin and Access-Control-Allow-Credentials: true. The wildcard origin is invalid in this mode, even if no cookies are actually sent. A required preflight must also approve credential use, and its allowed methods and headers must be listed explicitly rather than covered by a wildcard.
Use include only when cross-origin credentials are needed; the default fetch setting sends credentials only to the same origin. For XMLHttpRequest, the equivalent cross-origin setting is withCredentials = true. Server headers do not override the request's credential setting, cookie restrictions, or browser privacy rules.
When a server selects Access-Control-Allow-Origin according to the incoming Origin, include Vary: Origin so a shared cache does not reuse one origin's header for another origin. Preserve any existing Vary fields. Review CDN cache keys and error caching in the same path, especially where responses are reused among tenants or user sessions. Do not place sensitive, user-specific data in a publicly cacheable response merely because CORS has been configured.
Test the Intended Origins and Failure Paths
After the configuration change, retest from an approved browser origin with the real method, content type, custom headers, and credential mode. Capture both preflight and actual responses when preflight occurs. Confirm that JavaScript receives the intended response and that the application status is the expected one. If access is limited to named origins, also test an unapproved origin and confirm that the browser cannot read the response.
Test an error response and any redirect path separately. A redirect to a sign-in page or another hostname changes what the browser actually receives and may introduce a new CORS issue. If behavior differs between local development and production, compare the scheme, host, port, proxy route, and deployed headers rather than assuming the same origin. Record the browser, environment, date, expected result, and observed headers.







