concept developer in category cors

This is an excerpt from Manning's book CORS in Action: Creating and consuming cross-origin APIs.
Cross-Origin Resource Sharing, or CORS, was built to help solve this issue. Before CORS, developers would need to go to great lengths to access APIs from JavaScript clients in the browser. CORS enables cross-origin requests in a safe, standard manner. From a client’s perspective, CORS is awesome because it opens up a new world of APIs that previously wasn’t available to browser JavaScript. From a server’s perspective, CORS is awesome because it allows the server to open up its APIs to a new world of users.
If you’re building a public API, you want to open up access to as wide an audience as possible. Developers in other languages can easily use native libraries to make API requests. For example, a Python developer can use the httplib2 library to make HTTP requests to any server, regardless of where the request originates. The following snippet shows what a Python request to Flickr looks like. The httplib2 library doesn’t care whether the server is CORS-enabled; it indiscriminately makes the request and processes the response, as this sample code shows:
import httplib2 h = httplib2.Http(".cache") resp, content = h.request("http://www.flickr.com/photos/nypl/", "GET")JavaScript developers don’t have that advantage because the browser’s same-origin policy limits HTTP requests to a single domain. CORS enables JavaScript developers to use an API the same way a developer in another language could.
Because CORS is a standardized specification, it works in a consistent manner across sites. Developers need only learn about CORS once; then they can use the same techniques across all sites that support CORS.
While CORS requires new configuration on the server side, the client-side developer experience remains largely unchanged. JavaScript’s XMLHttpRequest object has been available in browsers for over 10 years. Developers make CORS requests using the same XMLHttpRequest object they are familiar with. There isn’t any new code for the developer to learn. From the developer’s perspective, same-origin and cross-origin requests look mostly the same. (There are some slight differences, which we’ll cover in chapter 2.)