How responseText and responseXML Work in AJAX

Introduction to Ajax

8. responseText and responseXML server responses

Using responseText and responseXML determines if the server responses in AJAX are in XML or plain text.  The field in which the response is returned depends upon what format is being used.  To determine the format, the programmer must either know what the server side script is set up to return or test the content type in the code.


The responseText property, which was introduced with Windows Internet Explorer 7, is used if the response from the server is not XML.  It has no default value and the response body is a string.


To receive a response body in an XML formatted text, use responseXML to parse the response as an XML object.  This property also has no default value and is compatible with Windows Internet Explorer 5, 7, 8, and 9.  In Internet Explorer 10 in Windows 8, the responseXML property is updated from XMLHttpRequest to return a native XML document by default.


The following code fragment illustrates these two fields by substituting alerts to display the appropriate field into the code, which was from the previous tutorial where the content type was tested.

if (cType == 'text/xml') {
} else if (cType == 'text/plain') {
} else {
alert('unknown content type');