1. Computing

Introduction to Ajax

8. responseText and responseXML

clr gif

In the last tutorial we looked at how we can determine whether the response is XML or plain text. The reason why we must know the answer to this (either by knowing what the server side script is set up to return or by testing the content type in our code) is that the response is returned in one of two different fields depending on which of these two formats it is that is being used.

The following code fragment illustrates these two fields by substituting alerts to display the appropriate field into the code from our last tutorial where we tested the content type.

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

©2014 About.com. All rights reserved.