concept xmlhttprequest in category cors

This is an excerpt from Manning's book CORS in Action: Creating and consuming cross-origin APIs.
The code starts by creating a new XMLHttpRequest object:
var xhr = new XMLHttpRequest(); if (!('withCredentials' in xhr)) { alert('Browser does not support CORS.'); return; } xhr.open(method, url);The first and last lines are the same for same-origin and cross-origin requests. The first line creates the XMLHttpRequest object, the last sets the HTTP method and URL.
The three middle lines highlight the difference between a same-origin-capable browser and a cross-origin-capable browser. If the browser fully supports CORS, the XMLHttpRequest object will contain a withCredentials property. You can use this property to check if the browser supports CORS. The preceding code alerts you if the browser doesn’t support CORS.
This chapter focuses on how to make CORS requests from JavaScript. It assumes you have an existing CORS-enabled API (such as the Flickr API, the GitHub API, or your own API) to make requests to (if you don’t have an existing CORS-enabled API, don’t worry. Part 2 looks at how to set one up). The chapter starts by defining what a cross-origin request is and which browsers support it. It then covers the main ways to make CORS requests, including the XMLHttpRequest and XDomainRequest objects. Next it looks at how the HTML5 <canvas> element uses CORS to load images. It ends with a look at jQuery support for CORS. The server-side details of CORS will be covered in chapter 3.
Second, the ajax method only supports XMLHttpRequest, and doesn’t support XDomainRequest. This means if you’re using jQuery and need support for CORS in Internet Explorer 8 or Internet Explorer 9, you’ll need to write code to fall back on the XDomainRequest object, or use a jQuery plugin that supports XDomainRequests, such as the one at https://github.com/jaubourg/ajaxHooks/blob/master/src/xdr.js.
Third, if you need to set the withCredentials property, you’ll need to use the xhrFields property, as shown in the following code snippet. The xhrFields property lets you set arbitrary fields on the XMLHttpRequest object:
$.ajax(url, { xhrFields: { withCredentials: true } });Fourth, jQuery doesn’t set the X-Requested-With request header on cross-origin requests. JQuery traditionally sets the X-Requested-With header on HTTP requests. This header is used by clients to indicate that a request is coming from an XMLHttpRequest object. The server receiving the request can look for the X-Requested-With header to determine where the request is coming from. JQuery always sets this header when making same-origin requests, but removes it from cross-origin requests. The reason is that setting custom request headers requires an additional server configuration step. Rather than force developers to make changes to their server, jQuery chose to drop this header. The following code snippet shows how to reenable the X-Requested-With header on cross-domain requests:
The XMLHttpRequest object handles the server’s response through a set of events. Events are functions that are invoked at specific moments in the lifecycle of an HTTP request. Events aren’t unique to the XMLHttpRequest object; when a user clicks a link on an HTML page, the browser fires a click event.