AJAX - Prop. of XMLHttpRes


AJAX - Properties of XMLHttpResponse

  • The XMLHttpRequest object has three important properties:
    • The responseText property
    • The readyState property
    • The onreadystatechange

1. The responseText Property

  • The XMLHttpRequest object preserves the data received from the server in its responseText property. This is transferred to a HTML element’s innerHTML attribute as follows :

Example :

document.getElementById("text").innerHTML =  xhttp.responseText; 

2. The readyState Property

  • This property contains the status of the server’s response. Below is the list of possible values : 

0: The request object is just declared.

1: The request has been initialized.

2: The request has been sent and its status is available.

3: The request is in process of receiving a response from the server.

4: The response is received completely.

Example :

if (xhttp.readyState == 4 && xhttp.status == 200) 
{
    document.getElementById("text").innerHTML =    xhttp.responseText; 
}

3. The onreadystatechange Property

  • The onreadystatechange property preserves a function to be triggered each time when the readyState property value changes.

Example :

xhttp.onreadystatechange = function() 
{
    if (xhttp.readyState == 4 && xhttp.status == 200) 
    {       
        document.getElementById("text").innerHTML =    xhttp.responseText; 
    }
};