Events in AJAX

:

Events are invoked when user perform some action. For example, when a request is sent to a server, we want to perform some actions based on that response. There are three important properties of the XMLHttpRequest object.

  • onreadystatechange
  • readyState
  • status

onreadystatechange:

It will stores a function and called automatically each time when the readyState property changes.

readyState:

This will holds the status of XMLHttpRequest object. The value of readyState will be change every time a request process completed. The possible values of status are as follows:

0 : request not initialized
1 : server connection established
2 : request received
3 : processing request
4 : request finished and response is ready to be handled

 status:

There are only two status:

200 : “ok”

404: Page not Found

In the onreadystatechange when readyStaus is 4 and status is 200 then the response is ready.

Example:

if (xmlhttp.readyState == 4) {

if (xmlhttp.status == 200)

{

document.getElementById(“message”).innerHTML = xmlhttp.responseText;

}

else {

alert(‘Something is wrong !!’);

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *