Hint
Browser blocks cross-origin requests by default; server opts in via CORS headers
The Same-Origin Policy (SOP) blocks web pages from reading resources from a different origin (protocol + domain + port combination).
// Same origin — all identical: protocol, domain, port
// https://app.com/page can read from https://app.com/api ✅
// https://app.com/page CANNOT read from https://api.other.com ❌
// CORS (Cross-Origin Resource Sharing):
// Server opts in by sending response headers
// Server response headers to allow access:
Access-Control-Allow-Origin: https://app.com // or * for all
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Allow-Credentials: true // if sending cookies
// Simple requests (GET, POST with basic headers) — no preflight
fetch('https://api.other.com/data');
// Preflighted requests — browser sends OPTIONS first
fetch('https://api.other.com/data', {
method: 'DELETE', // non-simple method
headers: { 'X-Custom': '1' } // non-simple header
});
// Browser sends: OPTIONS https://api.other.com/data
// → checks server allows it before actual request
// CORS does NOT apply to:
// - Server-to-server (Node.js fetch, cURL)
// - <img>, <script>, <link> tags (but limited access)
// - Same origin