+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

AJAX XMLHttpRequest


After creating the XHR object the next step is to use the methods and properties associated with it. So for implementing AJAX we use following:

XMLHttpRequest Methods

Below table gives a detailed description of the methods which are used with XHR:
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, async ) Specifies the method, URL, and other optional attributes of a request.The method parameter can have a value of "GET", "POST", or "HEAD". The async parameter specifies whether the request should be handled asynchronously or not. "true" means that the request will be generated asynchronously while "false" that the request is synchronous. For AJAX we set the parameter as true.
send( content ) Sends the request

XMLHttpRequest Properties

  • onreadystatechange
    An event handler for an event that fires at every state change.
  • readyState
    The readyState property defines the current state of the XMLHttpRequest object.The possible values for the readyState property are enlisted below:
    • 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 has 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 the 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").

Sample Code for Request Methods & Properties

Here we are creating an HTML file and using AJAX to fetch the content from a text file to be displayed in HTML page.
AJAX XMLHttpRequest

Is it Helpful?
Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US