Using DOM Scripting to Parse AJAX Responses with JavaScript
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.
Using DOM Scripting to Parse AJAX Responses with JavaScript - Listing the complete source code (Page 4 of 4 )
In consonance with the concepts deployed in the prior section, below you can see the complete signatures of the source files that comprise this AJAX-driven application. I suggest you take a close look at it to quickly learn the approach I used to parse plain text responses with a few DOM methods.
Here are all the signatures for all the source files that I mentioned before:
(definition of ajax_sample.htm file)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Above are all the source files that you need to get this sample AJAX application working seamlessly. It's extremely important to notice here how the PHP 5 script that actually fetches the database rows from the "Users" table uses a "|" delimiter to concatenate the different table fields before sending this data back to the browser to be parsed by the "displayResult()" JavaScript function that was discussed in the previous section. Quite simple, right?
A final note: as usual, you're free to modify all the code samples developed in this article, if you're interested in learning yet another approach to parse within the context of an AJAX application, the output of server-side scripts.
Final thoughts
In this second tutorial of the series I demonstrated how to use some standard DOM methods to process AJAX responses that are sent to the client in plain text, using the "responseText" property.
Nonetheless, this instructive journey has one more chapter, since I've not explained yet how to parse AJAX responses that are transmitted in XML format. As you might have guessed, this topic will be covered deeply in the last part of the series, so now you don't have any excuses to miss it!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.