XHTMLHttpRequest

 

Receiving data (handle response)

onreadystatechange

Sets a function to receive data returned by
the server after a request is sent......asynchronouse

  • Must be set before sending request

The following code defines a function for this
purpose (with an empty body for now)

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
         {
            // code for receiving response data
         }

readyState Property

The readyState property defines the current state of the XMLHttpRequest object.

Here are the possible values for the readyState propery:

State Description
0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is completed

You can also check the status of the response to see if it is Good.

The next thing to check is the status code of the HTTP server response. All the possible codes are listed on the W3C site. For our purposes we are only interested in 200 OK response.

if (http_request.status == 200) {
    // perfect!
} else {
    // there was a problem with the request,
    // for example the response may be a 404 (Not Found)
    // or 500 (Internal Server Error) response codes
}

responseText

Retrieve text data returned by the server

  • Type: DOMString (readonly)
         xmlhttp.onreadystatechange=function()
            {
               if (xmlhttp.readyState==4)
               {
                 document.getElementById(‘formentry’).value =
                 xmlhttp.responseText;
               }
           } 

responseXML

Retrieve document data returned by the server

  • Type: Document (readonly)
           var xmldoc=xmlhttp.responseXML.documentElement;
 

 

 

© Lynne Grewe