CORS
CORS lets a server tell the browser which websites can read its responses. It’s what allows a web app to read data from an API hosted on a different origin.
Also known as: Cross-Origin Resource Sharing
What CORS controls
Suppose your app runs at https://dashboard.example.com and fetches data from https://api.example.com. The two URLs belong to different origins: an origin consists of the scheme, host, and port, and here the hosts differ. Changing /reports to /settings wouldn't change the origin, but switching from HTTPS to HTTP would.
The browser's same-origin policy normally prevents a page's JavaScript from reading responses from another origin. Cross-Origin Resource Sharing (CORS) gives the responding server a way to grant that access through HTTP headers. The browser enforces the decision; CORS doesn't grant a user permission to perform an API operation.
Follow a request through the browser
For a cross-origin fetch, the browser sends an Origin header identifying the page's origin. The API can respond with Access-Control-Allow-Origin set to that exact origin. If the response passes the CORS check, JavaScript can read it. For a request without credentials, the server can also use * to let pages from any origin read the response.
Some requests need a preflight. For example, before sending a POST with Content-Type: application/json, the browser sends an OPTIONS request. It announces the intended method in Access-Control-Request-Method and lists headers that need approval in Access-Control-Request-Headers. The server responds with CORS headers allowing the origin, method, and headers. Only if that check succeeds does the browser send the POST, and the POST response must pass its own CORS check.
Example: reading a report
A plain fetch('https://api.example.com/report') from the dashboard is a GET that doesn't need a preflight. The API includes this header on the actual response:
Access-Control-Allow-Origin: https://dashboard.example.com
If the API selects the allowed origin based on the incoming Origin header, it should also send Vary: Origin so a shared cache doesn't reuse a response with CORS headers meant for a different origin.
Debugging and common mistakes
Check the Network panel first. If you see an OPTIONS request but no actual request, inspect the preflight response. If the actual request appears, inspect its response too: a proxy or server might return an error without the expected CORS headers. A successful curl request is useful for checking the server, but curl doesn't enforce CORS. Setting mode: 'no-cors' isn't a fix either: JavaScript can't read the body of the resulting opaque cross-origin response.
For a cross-origin request that should carry cookies, use credentials: 'include' in fetch. The API must return an explicit allowed origin rather than *, along with Access-Control-Allow-Credentials: true, for the browser to expose the response. Whether cookies are actually sent still depends on cookie attributes and browser policy.
What interviewers look for
Be ready to explain the difference between a request being sent and its response being read. A failed preflight stops the actual request. But a request that doesn't require a preflight can reach the server and change state even if the browser refuses to expose the response. That's why CORS doesn't replace server-side authorization or CSRF protection.