Same-Origin Policy vs CORS
People think CORS is a lock that blocks requests, and it is the other way round. The same-origin policy is the lock: it stops one site from reading another site's replies. CORS is a header a server sends to open that lock for sites it names. So a CORS error means the server did not give your site a key.
Same-Origin Policy (SOP)
A browser rule. It stops one site from reading what another site sends.
The same-origin policy is a browser rule that keeps sites apart. An origin is made of three parts of an address. They are the scheme, the host and the port. A page can freely read data from its own origin. It cannot read replies from another origin unless that site says yes. This stops a bad site from reading your email or bank pages in another tab.
Cross-Origin Resource Sharing (CORS)
A browser rule. It decides if a page on one site may read another site's data.
CORS is a browser rule that says if a page may read another site's data. By default, a browser stops a page from reading replies from another site. The server can relax that with a header that names the sites it trusts. If the header is missing or wrong, the browser shows a CORS error. It protects users. It does not protect the server from scripts or tools.
In one line
- Same-Origin Policy (SOP)
- A browser rule. It stops one site from reading what another site sends.
- Cross-Origin Resource Sharing (CORS)
- A browser rule. It decides if a page on one site may read another site's data.