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

Backbone of Ajax: XmlHttpRequest

By , 2 Nov 2007
Rate this:
Please Sign up or sign in to vote.

Introduction

"Ajax" is a new term which is spelled by almost all the web developers today! Does everyone know that it uses XmlHttpRequest internally? Many say "Yes", some people say "don't know". I am writing this article for all the developers who must understand XmlHttpRequest which I consider to be the backbone of Ajax.

What is Ajax?

Ajax is not really a technology, it is an architectural pattern. It is not another name for XmlHttpRequest, but a technique to call the server with the help of XmlHttpRequest, CSS, DOM, XML, HTML and other web entities.

Who is the Inventor of Ajax?

The answer is 'Nobody'. Although it is a design approach, no specific person or company owns it. "Jesse James Garrett" is the founder of this name. He used the combined web design approach in his applications, and he named it "Ajax". Google made Ajax famous, because we use Gmail and Google map interface on a day to day basis.

In this article, I'm going to concentrate on XMLHttpRequest, the backbone of Ajax. For further details on Ajax, you can refer to this link.

XmlHttpRequest

XmlHttpRequest is an object which is provided by almost all the browsers to enable scripted communication to the server. Using this, you can send or get any data from/to the server using JavaScript. Note that you do not need to post back your page here! Obviously, the end user is not aware about what is happening behind the scenes. This is the main reason we have the web application acting like a desktop application. Imagine Gmail, you can also use Gtalk from the browser, as we do in a desktop application.

Flow of the XmlHttpRequest

A Quick Example

I am not going to create an ASP.NET web project. Since XmlHttpRequest is common in all the web applications, everyone should understand what it is.

Here, I am going to explain how to call the HTML data from one page to another page.

Create the First HTML Page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Calling another page</title>
    <script language="javascript" type="text/javascript">

    //Creating the instance of the XmlHttpRequest
    // branch for native XMLHttpRequest object
    var client=null;
    if (window.XMLHttpRequest)
    {
        client = new XMLHttpRequest();
    } // branch for IE/Windows ActiveX version
    else if (window.ActiveXObject)
    {
        client = new ActiveXObject("Microsoft.XMLHTTP");
    }

    //sending information to server
    function doCall()
    {
        try
        {
        client.onreadystatechange = callBack;
        client.open("GET", document.getElementById("txtURL").value);
        client.send();
        }catch(ex)
        {
        alert(ex.message);
        }
    }

    //waiting and processing server response
    function callBack(response)
    {
        try
        {
            if(client.readyState == 4 && client.status == 200)
            {
            document.getElementById("panel").innerHTML=client.responseText;
            }
         }
        catch(ex)
         {
         alert(ex.message);
         }
    }
    </script>
</head>
<body>
<h4>Reading server response Using XMLHttpRequest</h4>
Server URL: <input id="txtURL" type="text" value="HtmlPage2.htm" />
<input type="button"  value="Call" onclick="doCall()" />
<br />[Output]
<hr />
<div id="panel"></div>
<hr />
</body>
</html>

Create the Second HTML Page

<b>Hello World</b>

Testing the Sample

Try to click that button; you can see "Hello World" text appear in bold, this is actually a response from the server. Try to give other URL in the text box; for example: http://www.codeproject.com, you will get an error message "Permission denied." Normally you can't call across the server because it's a security issue, your browser will not allow you to perform this operation. You can call only within a server.

Dynamic Contents

Using the above approach, you can also read a response from any ISAPI applications such as ASP, ASP.NET, PHP, and JSP.

How Does Ajax Use This?

It is really a time consuming job to write JavaScript to call the server, rather than if there is a Framework which does this job for us. This is the reason "Ajax" came to the industry, not only to automate the call, but also to automate everything with a standard and best approach. It creates a JavaScript method which calls our server method, so as developers, we do not need to write any JavaScript for XmlHttpRequest. This is the reason why many people do not know about the classic XmlHttpRequest way.

Does Ajax Create a Bottleneck in the Performance?

Really no. If we use it properly, there is no problem in the performance. We should not do everything on the web using JavaScript/Ajax because it runs on the client side. As a result, we will not have any control as we have on the server.

Please read this article regarding performance problems of Ajax.

License

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

About the Author

Manikandan.net
Web Developer
India India
"Manikandan Balakrishnan" Working as an IT consultant with LogicaCMG’s Global Service Delivery part-India, he has a good experience on Web/Win development (C#, Asp.net) and enterprise application integration (BizTalk) technologies. Prior to this he worked on world’s biggest e-learning product with Excel Soft Technologies, Mysore.
 
City: Coimbatore, TN
CreativeManix@gmail.com

Comments and Discussions

 
QuestionSave data using XmlHttpRequest PinmemberMember 322669930-Apr-12 3:42 
GeneralMy vote of 1 PinmemberMd. Mainuddin28-Jan-11 2:07 
Not informative
QuestionXmlHttpRequest vs. WebMethod? Pinmembertruongpham26-Aug-09 6:57 
AnswerRe: XmlHttpRequest vs. WebMethod? PinmemberManikandan.net26-Aug-09 20:21 
GeneralNice article PinmemberIslam94-Nov-07 2:18 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 2 Nov 2007
Article Copyright 2007 by Manikandan.net
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid