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:
Below table gives a detailed description of the methods which are used with XHR:
Cancels the current request.
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
An event handler for an event that fires at every state change.
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.
Returns the response as a string.
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.
Returns the status as a number (e.g., 404 for "Not Found" and 200 for "OK").
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.