XMLHttpRequest Object in Ajax

XMLHttpRequest (XHR) is an API available to web browser scripting languages such as JavaScript. It is used to send HTTP or HTTPS requests to a web server and load the server response data back into the script. Development versions of all major browsers support URI schemes beyond http: and https:, in particular, blob: URLs are supported.

An object of XMLHttpRequest is used for asynchronous communication between client and server.

It performs following operations:

  • Sends data from the client in the background
  • Receives the data from the server
  • Updates the webpage without reloading it.

Properties of XMLHttpRequest object

The common properties of XMLHttpRequest object are as follows:

onReadyStateChange-
It is called whenever readystate attribute changes. It must not be used with synchronous requests.

readyState-
represents the state of the request. It ranges from 0 to 4.
0 UNOPENED open() is not called.
1 OPENED open is called but send() is not called.
2 HEADERS_RECEIVED send() is called, and headers and status are available.
3 LOADING Downloading data; responseText holds the data.
4 DONE The operation is completed fully.

reponseText-
returns response as text.

responseXML-
returns response as XML

Methods of XMLHttpRequest object-

The important methods of XMLHttpRequest object are as follows:

void open(method, URL):
opens the request specifying get or post method and url.

void open(method, URL, async):
same as above but specifies asynchronous or not.

void open(method, URL, async, username, password):
same as above but specifies username and password.

void send():
sends get request.

void send(string):
send post request.

setRequestHeader(header,value):
it adds request headers.

Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports protocols other than HTTP (including file and ftp).

Create an XMLHttpRequest Object-

To create an instance of XMLHttpRequest, simply do this:

var myRequest = new XMLHttpRequest();

Old versions of Internet Explorer (IE5 and IE6) use an ActiveX Object:

var xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);

<<Previous <<   || Index ||   >>Next >>

32 Comments

  1. saquib July 1, 2014
  2. Dinesh Rajput July 2, 2014
  3. MallikarjunReddy Kamireddy July 2, 2014
  4. MallikarjunReddy Kamireddy July 2, 2014
  5. Bhargav July 2, 2014
  6. Dinesh Rajput July 3, 2014
  7. Changkeun You July 9, 2014
  8. Dinesh Rajput July 9, 2014
  9. albie.morken July 15, 2014
  10. Dinesh Rajput July 18, 2014
  11. Neha J July 22, 2014
  12. rag July 22, 2014
  13. Dinesh Rajput July 22, 2014
  14. rahul August 1, 2014
  15. Dinesh Rajput August 1, 2014
  16. Amol Jadhav August 3, 2014
  17. dfsdfsd August 14, 2014
  18. Ramesh Yallala September 4, 2014
  19. Dinesh Rajput September 4, 2014
  20. Ramesh Yallala September 4, 2014
  21. Ramesh Yallala September 4, 2014
  22. Dinesh Rajput September 4, 2014
  23. Dinesh Rajput September 4, 2014
  24. Ramesh Yallala September 4, 2014
  25. Dinesh Rajput September 4, 2014
  26. rahul October 6, 2014
  27. rahul October 6, 2014
  28. rahul October 6, 2014
  29. rahul October 6, 2014
  30. Dinesh Rajput October 6, 2014
  31. Binh Thanh Nguyen January 9, 2015
  32. venkat January 27, 2015