XMLHttpRequest object

The following standard code is used to create the XMLHttpRequest object. This code works with Internet Explorer (the first part) and Firefox browser (the second part). The XMLHttpRequest object has to be created in order to use the AJAX technology (AJAX = Asynchronous JavaScript And Xml).

function createXMLHttpRequest()
{
  if (window.ActiveXObject)
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest)
    xmlHttp = new XMLHttpRequest();
}

Once the XMLHttpRequest object is created, you can use its methods and properties shown in the following tables:

XMLHttpRequest Methods Description
abort() abort current request
getAllResponseHeaders() returns all the response headers as the key/value pairs
getResponseHeader("header") returns the value of a specific header (as a string)
open("method", "url") sets up a call to the url script by using GET or POST method
send(content) sends the request to the server
setRequestHeader("header", "value") sets up a specific header after calling the open() method

The method open() also takes three optional tailing arguments:

open(string method, string url, boolean async, string username, string password)

The async instructs the browser to make synchronous or asynchronous (default value) requests. The last two parameters allow to include user's name and password.

XMLHttpRequest Properties Description
onreadystatechange event handler that usually launches a JavaScript function
readyState 0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
responseText response from a server as a string
responseXML response from a server as an XML object
status status code from server:
200 = OK, 404 = not found, etc
statusText text version of the status code