CORS: Everything Software Developers Need to Know About CORS
TL;DR
Let’s imagine you’ve opened a website called “A.com” in your web browser. This web page intends to display an image retrieved from another website called “B.com.”
The problem here is related to security. For security reasons, when a web page is running on “A.com,” the browser cannot freely fetch images from a different source, such as “B.com.” This is known as the Same-Origin Policy.
Now, CORS comes into play. The “B.com” server explicitly grants permission to the browser to allow requests from “A.com.” The server does this by setting up permissions using headers, which the browser checks.
In other words, the “B.com” server responds with CORS headers that allow requests from the “A.com” origin in the request headers. The browser verifies this permission and safely fetches the image from “B.com.” This maintains security while enabling data sharing between two different sources.
In summary, CORS is a mechanism that defines rules between browsers and servers to safely exchange data between different origins. It allows web applications to utilize data from different sources or perform secure inter-server communication.