Advanced Topic 40 of 51 Web Development
CORS
Browser policy controlling cross-origin requests.
How it works
Cross-Origin Resource Sharing is a browser mechanism that relaxes the same-origin policy, which blocks scripts from reading responses from other origins, defined by scheme, host and port. A server opts in by sending headers such as Access-Control-Allow-Origin. For non-simple requests, the browser first sends a preflight OPTIONS request asking which methods and headers are allowed, and credentials need an explicit Access-Control-Allow-Credentials and a specific origin.
Step-by-step walkthrough
- A page on site A calls the API on site B.
- For risky requests the browser first sends an OPTIONS preflight.
- The API replies with Access-Control-Allow-Origin headers.
- If allowed the real request is sent.
- If not the browser blocks the response, the policy protects users.
Where it is used
Front ends hosted on one domain calling APIs on another, third-party widgets and public APIs configure CORS.
Common pitfall
CORS is enforced by browsers, not servers, and does not protect the API from other clients. Using a wildcard origin together with credentials is disallowed, and reflecting any origin blindly creates vulnerabilities. Missing headers on error responses and preflight handling cause confusing failures. Debug with the network panel, allow only needed origins and cache preflights with max-age.
Where it fits in the course
CORS is topic 40 of 51 in Web Development, rated Advanced. It follows OAuth 2.0. The next topic is Web Security Basics.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is CORS?
Where do people use CORS?
What is the most common mistake with CORS?
What should I learn before this topic?
How can I practice it?
Is CORS hard to learn?
How long does it take to learn CORS?
Which topics are related to CORS?
What should I study after CORS?
Related topics
- OAuth 2.0 (Web Development)
- Web Security Basics (Web Development)
- JWT (Web Development)
- Web Performance (Web Development)