Understanding CORS: Why Your API Request Failed
Every developer has hit the dreaded CORS error — your API works perfectly in Postman but fails in the browser. Cross-Origin Resource Sharing is a browser security feature that blocks requests from one origin (domain + protocol + port) to another unless the server explicitly permits it. When a request is "non-simple" (custom headers, PUT/DELETE methods, JSON content-type), the browser sends a preflight OPTIONS request first to check permissions.
Common causes of CORS errors: missing Access-Control-Allow-Origin header, incorrect allowed methods/headers, forgetting to handle OPTIONS requests, and credential misconfigurations. Server-side fixes involve setting proper headers (Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers).
Understanding CORS means understanding that it's not a bug — it's a security boundary protecting users from malicious cross-site requests.