0

The JavaScript logics below doesn't display the value from the demo_get.txt in ajax.html. Did I do something wrong?

ajax.html

<HTML>
<HEAD>
<script type="text/javascript">
    function ajaxRequest(){  
        var xmlhttp;
    	if(window.XMLHttpRequest){
           xmlhttp = new XMLHttpRequest();
           xmlhttp.open("GET","demo_get.txt",false);
           xmlhttp.send();
           document.getElementById("panel").innerHtml = xmlhttp.responseText;
        }else{
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           xmlhttp.open("GET","demo_get.txt",false);
           xmlhttp.send(); 
           document.getElementById("panel").innerHtml = xmlhttp.responseText;
        }
    }
</script>
</HEAD>
<TITLE>AJAX EXAMPLE</TITLE>
<BODY onload="ajaxRequest()">
   <div id="panel">Let Ajax alter this page</div>
</BODY>
</HTML>

demo_get.txt

HELLO AJAX APPLICATION
2
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by stbuchok
0

I made the changes but I can't see the results.

ajax.html

<HTML>
<HEAD>
<script type="text/javascript">
    function ajaxRequest(){  
        window.alert("Testing");
        var xmlhttp;
    	if(window.XMLHttpRequest){
           xmlhttp = new XMLHttpRequest();
           xmlhttp.open("GET","demo_get.txt",false);
           xmlhttp.send();
           document.getElementById("panel").innerHtml = xmlhttp.responseText;
        }else{
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           xmlhttp.open("GET","demo_get.txt",false);
           xmlhttp.send(); 
           document.getElementById("panel").innerHtml = xmlhttp.responseText;
        }
    }
</script>
<TITLE>AJAX EXAMPLE</TITLE>
</HEAD>
<BODY>
   <div id="panel"></div>
   <button type="button" onclick="ajaxRequest()">Change Content</button>
</BODY>
</HTML>

demo_get.txt

HELLO AJAX APPLICATION

Edited by solomon_13000: n/a

0

The code now works on mozilla but not in IE:

ajax.html

<HTML>
<HEAD>
<script type="text/javascript">
    function ajaxRequest(){  
        var xmlhttp;
    	if(window.XMLHttpRequest){
           try{  
               xmlhttp = new XMLHttpRequest();
           }catch(e){
               alert(e);
           }
           xmlhttp.open("POST","demo_get.txt",true); 
           xmlhttp.onreadystatechange=function(){
               if(xmlhttp.readyState==4){
                   document.getElementById("panel").innerHTML = xmlhttp.responseText;
               }
           }
           xmlhttp.send();
        }else{
           try{  
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           }catch(e){
               alert(e);
           }
           xmlhttp.open("POST","demo_get.txt",true);
           xmlhttp.onreadystatechange=function(){
               if(xmlhttp.readyState==4){
                   alert(xmlhttp.responseText);
                   document.getElementById("panel").innerHTML = xmlhttp.responseText;
               }
           }
           xmlhttp.send(); 
        }
    }
</script>
<TITLE>AJAX EXAMPLE</TITLE>
</HEAD>
<BODY>
   <div id="panel"></div>
   <button type="button" onclick="ajaxRequest()">Change Content</button>
</BODY>
</HTML>

demo_get.txt

HELLO AJAX APPLICATION
0

Why are you duplicating so much code?

Change it to this:

<HTML>
<HEAD>
<TITLE>AJAX EXAMPLE</TITLE>
<script type="text/javascript">
function ajaxRequest(){  

	var xmlhttp;

	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}
	else{
           
 		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

	xmlhttp.open("POST","demo_get.txt",true); 
           
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			document.getElementById("panel").innerHTML = xmlhttp.responseText;
		}
	}

	xmlhttp.send();
}
</script>

</HEAD>
<BODY>
   <div id="panel"></div>
   <button type="button" onclick="ajaxRequest()">Change Content</button>
</BODY>
</HTML>

What version of IE doesn't work? The example I gave you before on w3Schools works in all browsers. Also, put the title tag right after the starting head tag, not right before the ending head tag.

0

Version 9. Is it because i'm not testing the codes in a web server?

Edited by solomon_13000: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.