Click here to Skip to main content
Click here to Skip to main content

Simply AJAX

By , 9 Jul 2007
 

Screenshot - ScreenShot.gif

This article is intended to be a simple introduction to AJAX.

This example simply fills a table with values when the user makes a selection. It is intended to illustrate how easy it is to implement custom AJAX into your applications. There are many AJAX libraries available. However, if you have ever tried to customize AJAX to fit your unique application needs, you may have experienced frustration in getting it to do exactly what you needed. AJAX is a very simple technology, but understanding the basic functionality is key when implementing a custom solution. This example is intended to serve that purpose.

Using the code

This project was developed using Visual Studio 2005. The source code is commented and should be easy to follow. There are two ASPX pages in the solution. One is "passer" and the other is "catch." passer.aspx is the page that the user interacts with. catch.aspx is not seen, but prepares the XML and passes it back to passer.aspx.

This example uses an array to hold the data, but it could easily be adapted to use other data structures. The C# XmlTextWriter class makes turning any type of data into XML a breeze. The most challenging part was developing the JavaScript to populate the client control. Below is a generic function to create an XMLHttpRequest object:

var http_request=false;
function Begin()
{
    ////////////////////  Create ActiveX Object (Microsoft) ////////////////
    try 
    {
        http_request = new XMLHttpRequest();
    }
    catch (e)
    {
        try 
        {
            http_request = new ActiveXObject("MsXML_Container.XMLHTTP");
        } 
        catch (o)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed)
            {
                http_request = false;
            }
        }//End catch (e)
    }//End catch(o)

    if (!http_request)
    alert("Error initializing XMLHttpRequest!");
    ////////////////////////////////////////////////////////////////////////
}

Below, we are creating and defining the parameters to send via URL to catch.aspx before defining the return function:

    // URL 
    // This is the page that processes the request and then returns the XML
    // Append ?(selection) to the URL (dropdownlist value in this example)
    var url="Catch.aspx?choice=" + document.forms[0].ddlChoice.value;

    // Open Params Send, set Headers 
    http_request.open("GET",url,true);
    http_request.setRequestHeader('content-type', 'text/xml');
    http_request.setRequestHeader('Cache-Control:', 'no-cache');

    // Define the Javascript function to handle the response
    http_request.onreadystatechange = ShowResults; 
    http_request.send(null);    // Send the request to Catch.aspx 
}// End Begin  
             
function ShowResults()/***********************************************/
{
    var Request_Text=""; 
        // Variable to hold the XML/Text returned from Catch.aspx
    if (http_request.readyState == 4)
    if (http_request.status==200)
        // Make sure the response is valid and Complete
    {
        Request_Text=http_request.responseText;// Assign the response
        var XML_Container = 
            new ActiveXObject("Microsoft.XMLDOM");// Create XML Container
        XML_Container.async = false;
        XML_Container.loadXML(Request_Text);
            // Load the Container with the Text Response
           
/////////////////////////////////////////////////////////////////////////////
// At this point, you have successfully requested the information. It's been
// returned to you in text format (request.responseText) and loaded into
// an XML container (XML_Container). Now the parsing and populating of your
// desired control can begin. (A dynamically generated table in this case.)
/////////////////////////////////////////////////////////////////////////////

Points of interest

I originally developed this code to populate a datagrid with datasets retrieved from an SQL database via stored procedures. I tried many packaged AJAX libraries, but wasn't able to adapt them to the application's requirements. Learning how AJAX works behind the scenes has been a great help in designing rich user interfaces and fast response times. This example provides a fully (albeit simple) functional example of how AJAX can be used to provide a Desktop Application feel.

Yes, I know, this is a Microsoft IE example. It could, however, easily be modified to work with other browsers. Feel free to e-mail me with any questions, comments and suggestions.

History

  • 9 July, 2007 -- Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Clift Maples
Systems Engineer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralExcellentmemberashu84code11 Jul '07 - 1:59 
I am very new in Ajax and your example will boost my eagerness in Ajax .
I have one question : when we fetch page on client side and view source code we can see all java scripts , so any smart intruder can easily get some idea . How can we hide our java script ?? so that one can not easily track it .
 
Please help me .....
 
Thanks again for your nice example. Rose | [Rose] Smile | :)
 

GeneralRe: Excellentmembercliftmaples11 Jul '07 - 2:20 
No problem, just seperate the javascript into an javascript file (.js) in your project and then include the following in the .aspx page:
<script type="text/javascript" src="FileName.js"></script>
 
This will hide the script.
GeneralRe: Excellentmemberashu84code11 Jul '07 - 17:42 
Thanks for quick response.
your solution will help me lot .
But is there any way to encrypt or hide our javascript ?? so, when client save html and JS file , he/she can not read JS file .
 
Thanks .
Rose | [Rose] Blush | :O
GeneralRe: Excellentmemberrilov12 Jul '07 - 2:37 
Hope soon we will see an executable version of the java script .. i mean a coded format of the javascript supported by all browsers where we can put all our business logic and distribute across the net...
 
http://www.htmlguardian.org/help_main.html[^]
GeneralRe: ExcellentmemberNickolay Karnaukhov12 Jul '07 - 22:46 
Nobody prevent using ActiveX, COM and COM+. But it's just a guess.
 
------------------------------------------------------------
Want to be happy - do what you like!

GeneralRe: Excellent [modified]memberrilov13 Jul '07 - 0:13 
You are right...some of these technologies are made for windows operating systems...but an encrypted version of the java script will give more freedom to put complex business logic in it.
 
something similar like microsoft view state concept..Cool | :cool:
 
-- modified at 20:38 Friday 13th July, 2007
GeneralRe: Excellentmemberashu84code14 Jul '07 - 1:42 
Fully Agree With you .
 
just we/somebody have to make some Parser ( browser can support it for parsing of javascript) kind of thing , which will Encrypt Javascript and at runtime it will decrypat javascript and execute that codes in browser .(If i am wrong then Point out me. )
 
Sorry, i know only simple java scripts and yes now know how to use ajax with java script. Even i don't know fundamentals of " how javascript supported by browser " and " parsing of the javascript in browser "...
 
IF any body have any idea then please update me .
Rose | [Rose] Rose | [Rose] Rose | [Rose]
 

GeneralGood barebones examplememberleppie9 Jul '07 - 21:20 
Dunno why you have been voted so low, but this is AJAX in it's utter simplest form Smile | :)
 

GeneralGood samplememberthebossedgar9 Jul '07 - 10:11 
This is a very good sample of a simple AJAX. But you forgotted to close the braces of the ShowResults function.
GeneralRe: Good samplemembercliftmaples9 Jul '07 - 13:54 
It's only a partial source snippet, good catch though.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by Clift Maples
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid