Open DevTools, find the failed request in the Network panel, and compare its Origin request header with the server’s Access-Control-Allow-Origin response header. To permit CORS access, the server must return an appropriate response for the requesting origin. This is the first place to look when an AJAX request crosses domains and the browser reports a CORS error.
Cross Domain AJAX Starts With Two Origins
Suppose a page at https://app.example.com calls an API at https://api.example.com. An origin consists of scheme, host, and port. The hosts differ here, so the browser treats the request as cross-origin. Changing https to http or using a different port also changes the origin; a different path on the same scheme, host, and port does not.
CORS, or Cross-Origin Resource Sharing, lets the server tell a browser which origins may read a response. For cross domain AJAX, the browser sends the page’s origin with the request and checks the server’s response against that origin. The browser can block the page from reading a response even when the server received the request. A successful status alone does not establish that the browser will expose the result.
Here is an illustrative request from the page. It uses fetch, a modern way to make an asynchronous HTTP request from browser code:
const response = await fetch("https://api.example.com/profile");
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const profile = await response.json();
console.log(profile);
Run it inside an async function or a JavaScript module. A cross-domain request using AJAX APIs such as XMLHttpRequest follows the same browser sharing rules. The exact request and response headers reveal where the agreement breaks down. AJAX across domains relies on the same exchange of permissions.
Read the server’s answer
For this example, the API can allow CORS for the application by returning the following header on the actual response:
Access-Control-Allow-Origin: https://app.example.com
The value is the origin of the page making the request, not the API’s own origin or a URL with a path. To allow CORS for a public, non-credentialed resource, a server can instead return Access-Control-Allow-Origin: *. Choose that CORS allowance only when any origin may read the resource. For private data, specify trusted origins and keep normal authentication and authorization in place.
When an API chooses a response origin dynamically from a trusted allowlist, it should also send Vary: Origin so shared caches distinguish responses for different origins. Do not blindly echo every incoming origin: doing so can let untrusted sites read responses intended for users. Return the applicable headers on error responses as well; otherwise a useful HTTP error can appear to the page only as a CORS failure.
CORS JavaScript diagnostics can be confusing because the console reports a browser access problem while server logs may show an ordinary response. For a cross domain AJAX investigation, compare both sides: the server log shows what arrived, and the browser shows what the page was permitted to read.
Understand the preflight
Some requests require the browser to ask permission first. For example, a POST with Content-Type: application/json generally triggers an OPTIONS preflight. The browser sends the intended method and non-safelisted headers in Access-Control-Request-Method and Access-Control-Request-Headers. If the preflight fails, the browser does not send the actual request.
For an approved JSON request from the example app, the server could answer the preflight with a successful status and headers like these:
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Content-Type
The route, reverse proxy, or framework middleware must answer OPTIONS before a handler that only accepts POST rejects it. Configure the CORS permissions for methods and headers to match the actual request, and limit them to what the API supports. A cross-domain AJAX call may work without preflight when it meets the browser’s safelisted method, header, and content-type conditions; that does not remove the need for an appropriate response origin.
For AJAX cross domain requests, inspect the OPTIONS entry before the actual request. If the requested header appears in Access-Control-Request-Headers but is absent from the allow list, update the server policy or remove the unnecessary header. A successful preflight is only the first part: the actual response must also carry the appropriate origin header.
Include credentials deliberately
A cross-origin fetch does not include cookies by default. If the API needs a session cookie, the client can request credentials:
const response = await fetch("https://api.example.com/profile", {
credentials: "include"
});
The server must respond with the explicit allowed origin and Access-Control-Allow-Credentials: true. A wildcard origin cannot grant CORS access to a credentialed response. If a preflight occurs, its response must also allow credentials, although the preflight itself does not include them. Cookie attributes and browser privacy settings can still prevent a cookie from being sent; inspect the request and cookie information in DevTools.
Allowing CORS is a browser sharing decision, not a substitute for a server’s access control or defenses against unwanted state changes. Grant credentialed access only to origins that need it. An AJAX request across domains should be treated as an explicit relationship between the page and the API, not as a general permission for every site.
Trace the failure in DevTools
- For JavaScript CORS errors, open Network, enable preservation of the log, and repeat the request. Select the failed request and read its URL, method, status, and
Originunder Headers. - If there is an
OPTIONSentry, inspect its request method and requested headers. Compare those values with the preflight response’s allowed origin, methods, and headers. - Inspect the actual response too. Compare
Access-Control-Allow-Originwith the page origin, and checkAccess-Control-Allow-Credentialsif credentials are included. Look for redirects or an error response that bypasses the normal header configuration. - Read the Console message alongside the network details. Reproduce after the API or proxy configuration changes and confirm that the page can read the response.
For JavaScript requests, CORS enforcement cannot be switched off in another user’s browser. Setting mode: "no-cors" produces an opaque response that the page cannot read; it is not a way to retrieve API data. When JavaScript encounters a CORS failure on an API you do not control, ask its owner for an approved policy or use an authorized backend endpoint you control. That endpoint must enforce its own access rules.
The most likely failure is a missing or mismatched Access-Control-Allow-Origin header. In DevTools, compare the exact page origin with the response on the failing request, including any preflight. Configure the API or proxy to return the trusted origin on both relevant responses, then repeat the request and check that its body is readable.
