We depend on the onreadystatechange event which is triggered every time the readyState changes. The readyState property contains the status of the XMLHttpRequest. We simply need to check the readyState value and status prior to taking action on the data that was received.
HTML Example
XMLHTTPRequest Properties
readyState Status
0 – request not initialized1 – server connection established2 – request received3 – processing request4 – request completed and response is ready
To ensure that we do not take action on the data that is received from the web server until it has been completed, we simply check the status of the readyState property when the onreadystatechange event changes. When the readyState value is 4 and status value is 200, the response is ready.