William Oliver
William Oliver

Reputation: 1432

Using Ajax to change the inner content of a div

I am a beginner at Ajax, and I have this html code that is meant to change the inner content of a div by using xmlhttprequest to request different html addresses and put their contents in a div. What am I doing wrong? Here is the code:

    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript">
        var xmlhttp= null;
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }else{
            alert("You must be using a different browser.");
        }

        function makeRequest(serverPage,objID){

            var obj = document.getElementById(objID);
            xmlhttp.open("GET",serverPage);
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 100){
                    obj.innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.send(null);
        }
    </script>
</head>
<body onload="makeRequest ('content1.html','hw')">
    <div align="center">
        <h1>My Webpage</h1>
        <a href="content1.html" onclick="makeRequest('content1.html','hw'); return false;"> Page1</a> | <a href="content2.html" onclick="makeRequest('content2.html','hw'); return false;"> Page2</a> | <a href="content3.html" onclick="makeRequest('content3.html','hw'); return false;"> Page3</a> | <a href="content4.html" onclick="makeRequest(content4.html,hw); return false;"> Page4</a>
        <div id = "hw"></div>
</body>

Upvotes: 0

Views: 1761

Answers (5)

edp
edp

Reputation: 36

Generally, this looks OK to me.

However the xmlhttp.status == 100 check looks suspicious.

100 is an unusual HTTP status code. Typically web servers return 200 ("OK") on a successful request.

Try replacing the status == 100 check with status == 200.

For reference,please see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Upvotes: 2

lei.liu
lei.liu

Reputation: 9

function AjaxGet(url,helm)
{ 
  if (xmlhttp == null)
  {  if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp = new XMLHttpRequest();
    else  // code for IE6, IE5
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
  }

 xmlhttp.onreadystatechange = function()
 {   if (xmlhttp.readyState==4)
     {  if ( (xmlhttp.status==200) ||  (xmlhttp.status==304) )
          { document.getElementById(helm).innerHTML = xmlhttp.responseText; }
          else { document.getElementById(helm).innerHTML = xmlhttp.status + " " +xmlhttp.statusText; }
    }
 }

   xmlhttp.open("GET",url,true);
   xmlhttp.send();
}

Upvotes: 0

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

I guess it should be:

function makeRequest(serverPage,objID){
    xmlhttp.open("GET",serverPage);
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById(objID).innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}

Upvotes: 0

reergymerej
reergymerej

Reputation: 2417

Test for xmlhttp.status == 200 instead of 100.

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

Upvotes: 0

Murali N
Murali N

Reputation: 3498

change xmlhttp.status == 100 to xmlhttp.status == 200 check if this resolves your issue

and if you are try to run the page in IE try to add this line

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

Upvotes: 0

Related Questions