DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Understanding CORS: Cross-Origin Resource Sharing Explained
XLinkedInReddit
MediumFrontend Engineering

Understanding CORS: Cross-Origin Resource Sharing Explained

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • What is the Same-Origin Policy?
  • How CORS Works
  • Simple Requests
  • Preflight Requests
  • Common CORS Headers
  • The Credentials Gotcha
  • Debugging Tips

CORS errors are the bane of every frontend developer's existence. Let's demystify how it actually works.

What is the Same-Origin Policy?

Browsers block requests from one origin (protocol + domain + port) to another by default. This prevents malicious sites from reading data from your bank's API.

https://app.com → https://api.com = cross-origin (blocked)
https://app.com → https://app.com/api = same-origin (allowed)

How CORS Works

Simple Requests

GET, HEAD, or POST with standard headers go directly. The server responds with Access-Control-Allow-Origin.

Preflight Requests

For "complex" requests (PUT, DELETE, custom headers, JSON content-type), the browser sends an OPTIONS request first:

// Preflight request
OPTIONS /api/users
Origin: https://app.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: Content-Type, Authorization

// Preflight response
Access-Control-Allow-Origin: https://app.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 86400  // Cache preflight for 24h

Common CORS Headers

HeaderPurpose
Access-Control-Allow-OriginWhich origins can access (* or specific)
Access-Control-Allow-MethodsAllowed HTTP methods
Access-Control-Allow-HeadersAllowed custom headers
Access-Control-Allow-CredentialsAllow cookies (can't use with *)
Access-Control-Expose-HeadersHeaders the browser can read
Access-Control-Max-AgeHow long to cache preflight

The Credentials Gotcha

If you need cookies or auth headers cross-origin, you must set credentials: "include" on the client AND Access-Control-Allow-Credentials: true on the server. And you CANNOT use * for Allow-Origin — it must be a specific origin.

Debugging Tips

  • CORS errors are enforced by the BROWSER, not the server. cURL/Postman won't show CORS errors.
  • Check the Network tab for the preflight OPTIONS request
  • The actual error message in the console tells you exactly which header is missing

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • What is the Same-Origin Policy?
  • How CORS Works
  • Simple Requests
  • Preflight Requests
  • Common CORS Headers
  • The Credentials Gotcha
  • Debugging Tips

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.