Web development

Fix CORS at the server boundary

A browser CORS error usually means the remote response does not grant your page permission to read it. Fix the server policy or use an authorized backend; changing client code to no-cors does not expose the response body.

Worked example

Worked reference example

# Example response header for one allowed web origin:
Access-Control-Allow-Origin: https://example.com
Vary: Origin

# A JSON API should also return its correct content type.
Content-Type: application/json

How it works

The browser checks the requesting origin against the response’s permission headers. Some requests also require an OPTIONS preflight. Origin includes protocol, hostname, and port, so a local development origin is different from the deployed one.

Limits and mistakes

Only send these headers for origins and operations the server intends to allow. Credentialed requests have additional requirements and cannot use a wildcard origin in the same way. CORS is not authentication and does not stop non-browser clients from making requests.

Check the result

Inspect the failing request and any preflight in developer tools. Test from the actual page origin and confirm the server returns the intended headers on both success and error responses.

Sources

Reference documentation for the behavior used in this guide: