AJAX XMLHttpRequest

The XMLHttpRequest object is the heart of AJAX. It is available from Internet Explorer 5.5 which was released in July 2000, but not fully used until people started to talk about AJAX and Web 2.0 in 2005.

XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript and other web browser scripting languages to transfer and manipulate XML data to and from a web server using HTTP, establishing an independent connection channel between a web page’s Client-Side and Server-Side.

Despite the name, the use of XML is not required (JSON is often used instead) or even plain text is used, and the requests do not need to be asynchronous.

You have already got exposure on how to create a XMLHttpRequest object.

Here is the list of methods and properties supported by AJAX which you need to become familiar with.

XMLHttpRequest Methods

  • abort()
    Cancels the current request.
  • getAllResponseHeaders()
    Returns the complete set of HTTP headers as a string.
  • getResponseHeader( headerName )
    Returns the value of the specified HTTP header.
  • open( method, URL )
    open( method, URL, async )
    open( method, URL, async, userName )
    open( method, URL, async, userName, password )
    Specifies the method, URL, and other optional attributes of a request.The method parameter can have a value of “GET”, “POST”, or “HEAD”. Other HTTP methods, such as “PUT” and “DELETE” (primarily used in REST applications), may be possibleThe “async” parameter specifies whether the request should be handled asynchronously or not . “true” means that script processing carries on after the send() method, without waiting for a response, and “false” means that the script waits for a response before continuing script processing.
  • send( content )
    Sends the request.
  • setRequestHeader( label, value )
    Adds a label/value pair to the HTTP header to be sent.

XMLHttpRequest Properties

  • onreadystatechange
    An event handler for an event that fires at every state change.
  • readyStateThe readyState property defines the current state of the XMLHttpRequest object.Here are the possible values for the readyState propery:
    State Description
    0 The request is not initialized
    1 The request has been set up
    2 The request has been sent
    3 The request is in process
    4 The request is completed

    readyState=0 after you have created the XMLHttpRequest object, but before you have called the open() method.

    readyState=1 after you have called the open() method, but before you have called send().

    readyState=2 after you have called send().

    readyState=3 after the browser has established a communication with the server, but before the server has completed the response.

    readyState=4 after the request has been completed, and the response data have been completely received from the server.

  • responseText
    Returns the response as a string.
  • responseXML
    Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties.
  • status
    Returns the status as a number (e.g. 404 for “Not Found” and 200 for “OK”).
  • statusText
    Returns the status as a string (e.g. “Not Found” or “OK”).

Author: admin

Share This Post On

Pin It on Pinterest