What is CORS and why does the browser block certain cross-origin requests?
Short answer
CORS (Cross-Origin Resource Sharing) is a browser security mechanism that blocks a web page from reading responses from a different origin (domain/port/protocol) unless the server explicitly allows it via response headers like Access-Control-Allow-Origin — this protects users…
Advertisement
AModel answer
CORS (Cross-Origin Resource Sharing) is a browser security mechanism that blocks a web page from reading responses from a different origin (domain/port/protocol) unless the server explicitly allows it via response headers like Access-Control-Allow-Origin — this protects users from malicious sites silently reading data from other sites on their behalf.
// Server must respond with, e.g.:
// Access-Control-Allow-Origin: https://myapp.com
fetch('https://api.otherdomain.com/data'); // blocked unless server allows itReal-world example
Debugging why an API call works from Postman but fails with a 'CORS policy' error only in the browser.
Likely follow-ups
- What is a CORS 'preflight' OPTIONS request and when does the browser send one?
Related topics