Cross Origin Request Sharing(CORS) is, it is just a mechanism to by pass web browser's Single Origin Policy(SOP).
In simple terms, the web browser dosen't allow the frontend code for a website running on domainA to make calls to servers on domainB.
Eg. When you are on [facebook.com] on the browser, the frontend code for facebook cannot make calls to [icicibank.com]. This is what web browser's Single Origin Policy dictates. You can bypass this mechanism using CORS.
But why the hell was SOP needed in the first place?
Well, the answer is cookies!
Let's understand this with the same [facebook.com] and [icicibank.com] example. You see, whenever you interact with any website, the website takes your login information, and generally returns back a token of sorts and this token is stored in the web browser and is called a cookie. Now when you try to login to the see the website again, the token is detected and you are directly allowed to login without asking for your credentials again.
Before SOP existed these cookies were misused.
Eg.
Lets say you logged into [icicibank.com], checked your account balance and closed the web browser. Sometime later you tried to access [facebook.com].
Lets imagine facebook is an evil website for a second, now while you are on [facebook.com], the frontend code for facebook could be making AJAX(dynamic https request) to [icicibank.com].
Before SOP, web browsers allowed these requests to take place. Since these requests were executed in the background, you would never know whether these requests were made or not. And when these requests to icicibank's server were made through facebook's frontend code, the web browser would innocently pass the icicibank's cookie in the request. Allowing facebook to access your bank account and then misuse it for evil purposes.
And this is why SOP was created!
Now, there are some APIs that the developers have worked hard on and want to be public in nature. CORS allows for that to happen.
Access-Control-Allow-Origin
on the server end? How would cross origin requests even get there if the browser won't allow it? – Mart