XMLHttpRequest Overview


Sometimes the Web can be viewed as an information space where applications exchange messages about various resources. JavaScript client code generates and modifies HTTP requests by means of techniques known under the umbrella term of Ajax. The Ajax is an established acronym for asynchronous JavaScript and XML. Nowadays Ajax APIs are not reduced to XML only: modern interfaces allow client-side routines to manipulate both trivial markup and complex binary objects on the Web. The pivot point of the Ajax functionality is the XMLHttpRequest API. JavaScript frameworks usually build more elaborate solutions on top of the "raw" XHR: for example, goog.net namespace from the Google Closure library has a set of classes for handling XMLHttpRequests, YUI Library uses Connection Manager for the client-server interaction, and jQuery contains a number of convenient methods to create and parse HTTP requests with a single line of code. However, a basic understanding of processes "under the hood" of the Ajax scripting can help the developer to use HTTP client APIs more efficiently.

HTTP Methods

The default constructor for XMLHttpRequest does not require any parameters:

var xhr=new XMLHttpRequest();

A valid HTTP request starts with a request line. The line contains information about a method to be performed on the requested Web resource, an absolute or relative URL of the resource and the version of the applied protocol:

GET http://example.com/ HTTP/1.1

The required HTTP method (it is also called HTTP verb) and the location of the resource are supplied as the arguments of the XHR open() method:

xhr.open("GET", "http://example.com/"); // absolute URL

Standard HTTP verbs include OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE and CONNECT. Browser vendors restrict the range of methods allowed for the XHR generation: e.g., an attempt to use the TRACE method with an XMLHttpRequest instance will raise a security error on the client side. Using such methods as TRACK or CONNECT is not allowed, either. In addition, Web servers are usually configured to filter out potentially harmful requests, so a server may ignore requests based on the use of DELETE or PUT methods.

Requests are asynchronous by default: it means that the flow of the code execution is returned to the calling program before the response is loaded. Synchronous communications with a remote server are enabled by passing a new argument to the open() method:

xhr.open("GET", "http://example.com/", false); // asynchronous flag as false boolean value

Additional parameters of the open() method are user name and password required for HTTP authentication.

xhr.open("GET", "http://example.com/", true, "user", "password"); // asynchronous request with credentials

After a new XMLHttpRequest is created, the send() method is called:

xhr.open("GET", "http://example.com/"); // asynchronous request
xhr.send();

The request above does not bear any payload and can have the following structure:

GET http://example.com/ HTTP/1.1 request line
Accept: */* request header
Referer: http://example.com/code/js/xhr/examples.html request header
Accept-Language: en-US request header
Accept-Encoding: gzip, deflate request header
User-Agent: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko request header
Host: example.com request header
Connection: Keep-Alive general header

Windows HTTP Services

Windows administrators used client scripts to build HTTP requests long before the standard XHR APIs were implemented in major browsers. The modern XMLHttpRequest itself inherits its methods and properties from the original ActiveX object available on the Windows platform. Internet Explorer 11 supports the standard XHR, however, there are additional interfaces enabling the communication with HTTP servers outside the Web-based environment. These are COM XMLHTTP and WinHTTPRequest automation components. These objects can be employed in system scripts written in languages targeting Windows Script Host:

var xhr=new ActiveXObject("Msxml2.XMLHTTP.6.0");
xhr.Open("GET", "http://example.com/", false); // synchronous request
xhr.Send(); // script will wait for response
WScript.Echo(xhr.getAllResponseHeaders());

The script above is launched as a console app by invoking cscript.exe xhr.js //NoLogo. The same operation with WinHttpRequest in VBScript looks like this:

Dim request
Set request=CreateObject("WinHttp.WinHttpRequest.5.1")
request.open "GET", "http://example.com/", False
request.send
WScript.Echo request.getAllResponseHeaders()

Besides traditional usage, the COM components can be passed to the .NET managed runtime, e.g., in Windows PowerShell scripts:

$request=New-Object -ComObject "WinHttp.WinHttpRequest.5.1"
$request.Open("GET", "http://example.com/", 0)
$request.Send()
Write-Host $request.GetAllResponseHeaders()