concept response in category cors

This is an excerpt from Manning's book CORS in Action: Creating and consuming cross-origin APIs.
3. The server includes HTTP headers in the response that indicate whether the request is allowed.
4. If the request is allowed, the browser sends the response to the client code.
If the headers returned by the server don’t exist, or aren’t what the browser expects, the response is rejected and the client can’t view the response. In this way, browsers can still enforce the same-origin policy on servers that don’t allow cross-origin requests. Now that you have a sense of what CORS is, let’s turn our attention to making a CORS request.
Notice how the response has a header set to Access-Control-Allow-Origin: *
. The Access-Control-Allow-Origin header is the magic behind CORS. The server uses this header to indicate that cross-origin requests are allowed. The Access-Control-Allow-Origin header must always be present on a CORS response, but it’s just one of many headers that can be used to configure CORS behavior. Part 2 of this book will cover these headers in greater detail.
Once the browser receives the response, it checks the response headers to verify that the cross-origin request is valid. If the request isn’t valid, the browser will log an error to the console, then fire the XMLHttpRequest’s onerror event. But because the response here is valid, the browser fires the XMLHttpRequest’s onload event:
xhr.onload = function() { var data = JSON.parse(xhr.responseText); if (data.stat == 'ok') { var photosDiv = document.getElementById('photos'); photosDiv.innerHTML = ''; var photos = data.photos.photo; for (var i = 0; i < photos.length; i++) { var img = document.createElement('img'); img.src = photos[i].url_q; photosDiv.appendChild(img); } } else { alert(data.message); } };This code parses the response text into a JavaScript Serialized Object Notation (JSON) object, grabs the images from the object, and displays them on the page. In addition to the response text, the XMLHttpRequest object also has properties for the HTTP status, HTTP status text, and methods that retrieve response headers.
. The Access-Control-Allow-Origin header is the magic behind CORS. The server uses this header to indicate that cross-origin requests are allowed. The Access-Control-Allow-Origin header must always be present on a CORS response, but it’s just one of many headers that can be used to configure CORS behavior.