XMLHttpRequest enhancements

11/03/2017

5 minutes to read

In this article

Internet Explorer 10 and Windows apps using JavaScript introduce several enhancements to the XMLHttpRequest object to provide greater support for emerging standards and to more easily support common developer scenarios. These include the following:

Downloading and uploading binary files, such as images, video, and audio without the use of plug-ins.

Download streaming of multimedia content without the use of plug-ins.

Improved insight into the status of XMLHttpRequest (XHR) operations.

Improved interoperability with other browsers.

These changes are described in more detail in the following sections.

Binary Object upload and download

When the responseType property of an XMLHttpRequest object is set to "blob", the data associated with the request is treated as binary data. This affects the value of the response property for download requests (for instance, GET).

The following example shows how to use XHR to download an image to a blob object and then assign the result to an image element on the webpage.

Comet streaming support

Internet Explorer 10 supports multipart HTTP streaming (also known as Comet streaming) by allowing the responseText property to be read while the response to an XMLHttpRequest request is loading, as shown in the following example:

In earlier versions of Windows Internet Explorer, the responseText property can be read only when the readyState property is set to done.

Be aware that the responseText property returns the full value of the response read to date. If you wish to handle individual packets of the response as they are received, you can use the progress event (described in the next section) or track the length of responseText in each iteration of the readyStateChange event, as shown in the following example:

By default, the same-site origin policy prevents websites from requesting resources from servers on other domains. However, browsers that support CORS for XHR requests can access resources from other domains if the appropriate administrator chooses to allow such requests.

When a webpage makes an XHR request,Internet Explorer sends an origin header to the target server; the header contains the protocol scheme of the request (either http:// or https://) and the hostname for the webpage making the request. If the target server approves the request, it returns an Access-Control-Allow-Origin header and the request is allowed to proceed.

XMLHttpRequest objects now support a withCredentials property, which allows XHR requests to include authorization mechanisms. For more information, see the XMLHttpRequest Level 2 specification.

The withCredentials property can be used to detect CORS support, as shown in the following example.

Microsoft Edge improves networking performance by caching resources downloaded via CORS-enabled XHR operations in the same manner as it would if they had been downloaded directly. Previously, under many circumstances, caching would be disabled for such items. This new behavior allows more resources to be loaded from the local disk on subsequent requests, reducing network use and improving load times.