Using DOM Scripting to Parse AJAX Responses with JavaScript
(Page 1 of 4 )
For web developers who need to build AJAX-driven applications, parsing the output generated by a server-side script using the "responseText" and "responseXML" properties of HTTP XML Request objects can be a challenging task. This is particularly true in those cases where it's difficult to determine which format should be used for delivering this output in a readable form to end users. This article series shows you several good solutions.
Introduction
Welcome to the second part of the series that began with "Parsing AJAX responses with JavaScript." In response to the aforementioned issue, this series walks you through the development of several JavaScript-based approaches for processing as plain text or in XML format, the responses sent by a web server after performing an AJAX HTTP request.
Now that I've introduced the primary objective of this series, let me do a brief recap of the topics covered in the first article. This will give you a better idea of how that tutorial will link with this one.
In the preceding article of the series I illustrated in a hopefully friendly fashion how to use the neat "innerHTML" JavaScript property, from within the context of a simple AJAX application, to parse a group of MySQL database records which had been previously fetched with a couple of PHP classes.
As you'll probably recall, this parsing process was indeed very straightforward. It was limited to extracting the mentioned database rows from a sample MySQL table with PHP, and then populating a determined (X)HTML container with the rows in question by using the handy "innerHTML" property.
Of course, this approach, as everything in life, has some pros and cons. On one hand the utilization of the mentioned JavaScript property doesn't require creating new (X)HTML elements on the fly (via DOM scripting) to display server-side data on a concrete web document. On the other hand, however, the property in question isn't considered standard by the W3 Consortium, which could be a problem if you're a web developer who strongly prefers to stick to the W3C standards.
In either case, using the "innerHTML" JavaScript property is probably the simplest method for parsing the response of an AJAX-based HTTP request returned from the web server as plain text. This doesn't mean that the topic ends here. There are a few other parsing approaches that deserve a close look. Therefore, in this second tutorial of the series I'll show you how to parse the data coming from an AJAX request by utilizing a completely standard approach, in this case via some DOM scripting.
Indeed, the experience will be educational. By the end of this article you'll have a more solid background on how to parse an AJAX request returned in plain text, by using the functionality provided by the DOM. So, are you prepared to keep learning more on this exciting topic? Let's get going!
Next: Fetching some database records with AJAX >>
More JavaScript Articles
More By Alejandro Gervasio