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 - Fetching some database records with AJAX (Page 2 of 4 )
In order to demonstrate how to use the DOM for parsing a web server response sent in plain text from inside the content of an AJAX application, first I'm going to recreate the development scenario that I used in the first article of the series.
As you'll probably recall, I created a sample "Users" MySQL database table, which was previously populated with a few simple records. Then, I defined a pair of MySQL processing classes in PHP 5, which came in handy for retrieving these records and sending them back to the client in plain text.
The interaction between the classes was performed via AJAX. Here's the definition of the JavaScript function that I used to trigger the JavaScript-based HTTP requests:
function sendHttpRequest(url,callbackFunc,respXml){
Assuming that the signature of the above "sendHttpRequest()" JavaScript function is very familiar to you at this time, the next step consists of listing the corresponding definitions of the PHP 5 classes that I defined to interact with MySQL.
Here's how this pair of MySQL processing classes looked originally:
class MySQL{
private $host;
private $user;
private $password;
private $database;
private $conId;
// constructor
function __construct($options=array()){
if(!is_array($options)){
throw new Exception('Connection options must be an array');
}
foreach($options as $option=>$value){
if(empty($option)){
throw new Exception('Connection parameter cannot be empty');
throw new Exception('Error counting affected rows');
}
return $rows;
}
}
All right, now that you've recalled how the previous MySQL handling classes worked, the last thing that I'm going to do in this section will be listing the structure of the sample "Users" database table, whose rows were displayed at a predefined time interval on the browser, by using an AJAX-based HTTP request.
Basically, this database table was defined like this:
Id First Name Last Name Email Comments
1 Alejandro Gervasio alejandro@domain.com MySQL is great for building a search engine
2 John Williams john@domain.com PHP is a server side scripting language
3 Susan Norton susan@domain.com JavaScript is good to manipulate documents
4 Julie Wilson Julie@domain.com MySQL is the best open source database
So far, so good, right? At this point I've recreated the development scenario that I showed you in the first article of the series. So what's next? Well, basically I'll keep the logic of this AJAX application nearly the same, which implies that the application will be tasked with displaying the database rows listed previously in a predefined sequence, as you learned earlier.
Here, however, is where the difference comes in: I'm going to define a brand new JavaScript function, which will be responsible for parsing the data coming from the web server by using only the DOM, instead of utilizing the non-standard "innerHTML" JavaScript property. By doing this, you'll hopefully learn how to implement a standard approach for parsing database information fetched via an AJAX-based request.
To see how this brand new JavaScript function will be defined, you'll have to visit the following section and keep reading. It's just one click away.