Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Usability

How To Use AJAX

  • Print
  • + Share This
Like this article? We recommend

Parsing the Response

The real work begins when you're ready to parse the response from the request object. This is where you actually start working with the data that you requested. For testing purposes during development, the responseText and responseXML properties can be used to display the raw data from the response. To begin accessing nodes in the XML response, start with the request object that you created, target the responseXML property to retrieve (you guessed it) the XML from the response. Target the documentElement, which retrieves a reference to the root node of the XML response.

var response = request.responseXML.documentElement;

Now that you have a reference to the root node of the response, you can use getElementsByTagName() to retrieve childNodes by their node names. The following line locates a childNode with a nodeName of header:

response.getElementsByTagName('header')[0].firstChild.data;

Using firstChild.data allows you to access the text within the element:

response.getElementsByTagName('header')[0].firstChild.data;

Here's a complete example of how to write the code:

var response = request.responseXML.documentElement;
var header = response.getElementsByTagName('header')[0].firstChild.data;
document.getElementById('copy').innerHTML = header;
  • + Share This
  • 🔖 Save To Your Account