JavaScript
  Home arrow JavaScript arrow Page 3 - Using DOM Scripting to Parse AJAX Response...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Using DOM Scripting to Parse AJAX Responses with JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-11-06

    Table of Contents:
  • Using DOM Scripting to Parse AJAX Responses with JavaScript
  • Fetching some database records with AJAX
  • Using DOM scripting to parse plain text responses with AJAX
  • Listing the complete source code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this second chapter of this series, you’ll learn how to use the most common...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT