AJAX Server Response


Server Response
To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.

Property Description
responseText Gets the response data as a string.
responseXML Gets the response data as XML data.


The responseText Property
If the response from the server is not XML, use the responseText property. The responseText property returns the response as a string, and you can use it accordingly:
     document.getElementById("myDiv").innerHTML
   = xmlhttp.responseText;
Let AJAX change this text.



The responseXML Property
If the response from the server is XML, and you want to parse it as an XML object, use the responseXML property. The following AJAX requests the file CD_catalog.xml and parse the response:

Get my CD collection.


<script type="text/javascript">
  function loadXMLDoc( ) {
    var xmlhttp;
    if ( window.XMLHttpRequest ) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest( );
    }
    else {
      // code for IE6, IE5
      xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
    }
    xmlhttp.onreadystatechange = function( ) {
      if ( ( xmlhttp.readyState == 4 ) && 
           ( xmlhttp.status     == 200 ) ) {
        xmlDoc = xmlhttp.responseXML;
        txt = "";
        x = xmlDoc.getElementsByTagName( "ARTIST" );
        for ( i=0; i<x.length; i++ ) {
          txt = txt + x[i].childNodes[0].nodeValue;
        }
        document.getElementById("myDiv_XML").innerHTML = txt;
      }
    }
    xmlhttp.open( "GET", "CD_catalog.xml", true );
    xmlhttp.send( );
  }
</script>




      A man just assaulted me with milk, cream, and butter.    
      — How dairy