Click here to Skip to main content
15,896,557 members
Articles / Web Development / HTML

Backbone of Ajax: XmlHttpRequest

Rate me:
Please Sign up or sign in to vote.
2.80/5 (12 votes)
2 Nov 2007CPOL3 min read 54.5K   439   24  
An article on XmlHttpRequest and Ajax
<!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 an another page</title>
    <script language="javascript" type="text/javascript">
    
    //Creating the instanse 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 responseUsing 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>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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