JavaScript
  Home arrow JavaScript arrow Page 2 - JavaScript Remote Scripting: Reading Data ...
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  
Moblin 
JMSL Numerical Library 
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

JavaScript Remote Scripting: Reading Data From the Server
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 22
    2005-09-21

    Table of Contents:
  • JavaScript Remote Scripting: Reading Data From the Server
  • A non-standard approximation: reading server data with AJAX
  • Checking asynchronous http requests: taking a look at the "statusChecker()" function
  • Displaying file contents: defining the "createDataContainer()" and "displayData()" functions
  • Reusing the source code: listing the full script

  • 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


    JavaScript Remote Scripting: Reading Data From the Server - A non-standard approximation: reading server data with AJAX


    (Page 2 of 5 )

    As I said in the introduction of the article, the first client-based remote scripting method that I'll explain, in order to retrieve data from the server, is AJAX. The starting point to illustrate the functionality of this approach for making http requests will be developing an extensible JavaScript application, which fetches data from a text file and builds a simple news rotator.

    Since the script is flexible enough, at the end of the example you'll be able to modify the source code to extend its functionality for working directly with database tables.

    Before I start writing down the relevant functions that integrate the application, I'd like to go over a few basic requirements of which you should be aware. Since the script will implement XMLHttpRequest objects, I'll use exceptions to handle the pertinent browser incompatibilities and instantiate cross-browser objects, thus a basic knowledge of "try-catch" block combinations will be required.

    Having said that, the first function to be written, tasked with sending http requests for a given file, is "sendRequest()" and looks like this:

    function sendRequest(doc){
        // check for existing requests
        if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
            xmlobj.abort();
        }
        try{
            // instantiate object for Firefox, Nestcape, etc.
            xmlobj=new XMLHttpRequest();
        }
        catch(e){
            try{
                // instantiate object for Internet Explorer
                xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e){
                // Ajax is not supported by the browser
                xmlobj=null;
                return false;
            }
        }
        // assign state handler
        xmlobj.onreadystatechange=stateChecker;
        // open socket connection
        xmlobj.open('GET',doc,true);
        // send request
        xmlobj.send(null);
    }

    Now that the above function has been defined, let's spend a few minutes discussing its corresponding logic.

    Stripped down to its bare bones, the "sendRequest()" function performs two clearly delineated tasks. The first one consists of instantiating an XMLHttpRequest object, by wrapping the lines responsible for object creation into a couple of "try" blocks. As I said previously, cross-browser object instantiation is handled by native JavaScript exceptions, so the "try-catch" block combination takes care of creating the appropriate objects in all the possible cases.

    On top of the function's code I included a checking line to verify whether there are existing connections. As a result, whenever the function is called, any existing requests will be cancelled by using the abort() method. This might seem like a trivial task, but checking code is required to handle the limitations imposed by JavaScript http requests, since they allow you to work with only one object at a time.

    By returning to the tasks performed by the function, once the appropriate object has been created, the "stateChecker()" function is assigned to the "onreadystatechange" event handler. Then, a socket connection is opened for performing a GET request, in order to retrieve the contents of the file passed in as an argument to the function. Finally, the request is sent out by using the send() method.

    Now that you know how the "sendRequest()" function does its thing, first by creating cross-browser XMLHttpRequest objects, and second by making GET requests to retrieve the contents of a given file, it's time to have a look at the next function, "statusChecker()", which is tasked with verifying the status of the current request.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This first tutorial introduces the basics for fetching files from the server through...
       · Your code assumes that AJAX is not possible in a non-ActiveX IE.According to...
       · Hello Kae,I wrote the script by assumming the availability of ActiveX in IE6....
       · I thank you for your really cool article about AJAX. For me it's kinda new and...
       · Hi there. His follow up article should come 2 weeks after this one. Usually, a...
       · Hello,Many thanks for the kind words about the introductory AJAX series. Of...
     

    JAVASCRIPT ARTICLES

    - 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
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






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