Home arrow JavaScript arrow Page 3 - Using DOM Scripting to Parse AJAX Responses with JavaScript
JAVASCRIPT

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.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
November 06, 2007
TABLE OF CONTENTS:
  1. · Using DOM Scripting to Parse AJAX Responses with JavaScript
  2. · Fetching some database records with AJAX
  3. · Using DOM scripting to parse plain text responses with AJAX
  4. · Listing the complete source code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using DOM Scripting to Parse AJAX Responses with JavaScript - Using DOM scripting to parse plain text responses with AJAX
(Page 3 of 4 )

As I mentioned in the section that you just read, I'm going to define a brand new JavaScript function for parsing all of the user data fetched from the corresponding "Users" database table that I showed you earlier.

This function, which I called "displayResults()" has the following signature:

function displayResults(userData){

 var datacont=document.getElementById('datacontainer');

   if(!datacont){return};

 var userData=userData.split('|');

   if(userData.length>0){

 var cont=document.getElementById('container');

   if(cont){cont.parentNode.removeChild(cont)};

 var div=document.createElement('div');

    div.setAttribute('id','container');

 for(var i=0;i<userData.length;i++){

 var h3=document.createElement('h3');

  var par=document.createElement('p');

h3.appendChild(document.createTextNode(userData[i].split(':')
[0]));

par.appendChild(document.createTextNode(userData[i].split(':')
[1]));

  div.appendChild(h3);

  div.appendChild(par);

}

 datacont.appendChild(div);

}

setTimeout("sendHttpRequest
('get_user_data.php','displayResults')",5*1000);

}

As you can see, the above "displayResult()" JavaScript function behaves nearly the same as the one created in the previous article of the series. However, despite this similarity, it parses all of the user data by using some common DOM methods. In this case, once the correct database row has been fetched from the "Users" database table, its different fields are split up by using a simple "|" delimiter before sending it back to the browser.

On the client side, the prior "displayResults()" function expects the data to come in that concrete format. It uses the DOM to display this data using some <h3> headers and a few paragraphs, which are dynamically appended to the wed document tree.

Finally, the function fetches cyclically from MySQL the different database rows every five seconds, by utilizing a conventional JavaScript timer.

Do you see how easy it is to parse server-side data sent in plain text using some DOM scripting, from inside the context of an AJAX application? I hope you do! This approach requires writing a few additional lines of code, but it uses fully standard DOM methods.

So far, so good. At this stage I demonstrated how to utilize a completely standard approach to parsing data coming from the web server, based upon an AJAX-based HTTP request. Nonetheless, this sample AJAX application looks rather disjointed, since I showed you each of its pieces separately.

Therefore, in the last section of this tutorial I'm going to list the complete source code of the AJAX application, so you can see more clearly how its different modules link with each other.

As I said before, this will be done in the next few lines, thus click on the link below and keep reading.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials