04 - Processing server response

Processing server response

Every XMLHttpRequest object transition between 5 stages to complete a request. The XMLHttpRequest object has a property called readyState that you can check to verify in which stage your request is:

Value

State

ReadyState Description

0

UNSENT

Once the XMLHttpRequest object is instantiated. No method has been called.

1

OPENED

Once the open method has been executed and it was successful.

2

HEADERS_RECEIVED

Once the send method has been executed and it has received the HTTP response headers.

3

LOADING

After the Headers are received and while the HTTP response content is being loaded.

4

DONE

Once the HTTP response content is completely loaded.

Once the readyState is 4, it means that your request is completely processed and you could go ahead and perform the update you need in your webpage.

The XMLHttpRequest object has a property used for checking the readyState on every status change. This property is the onReadyStateChange, so when a response is received this function will be called on each status change and when the response is complete (in DONE state) you can execute the code to update the webpage

xmlHTTPRequest.onreadystatechange=function()
{
    if (xmlHTTPRequest.readyState==4 && xmlHTTPRequest.status==200)
    {
    //Execute the update of the webpage
    }
}

Like us on Facebook