Link to home
Start Free TrialLog in
Avatar of Wassini
WassiniFlag for Denmark

asked on

Ajax onReadyStateChange problem

I have two functions:
Ajax: this is working!
With a handler (onReadyStateChange). This is NOT working! It only calls the handler function once with no status or readyState!

I have tested in IE + Chrome

Any clue?

Btw: The ASP file is just a dummy:
response.write "OK"
<script>
	function Ajax() {
		var xmlHttp = new XMLHttpRequest();
		
		xmlHttp.open("GET", "test.asp", false);
		xmlHttp.send();
		alert("Ajax:"  + xmlHttp.responseText);
	}

	function DynAjax() {
		var xmlHttp = new XMLHttpRequest();
		
		xmlHttp.open("GET", "test.asp", true);
		xmlHttp.onReadyStateChange = handler();
		xmlHttp.send();
	}	


	function handler() {
		if(this.readyState == 4 && this.status == 200) {
			alert("DynAjax: " + this.responseText);
		}
	}
</script>

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

try

function DynAjax()
{
            var xmlHttp = new XMLHttpRequest();
            
            xmlHttp.open("GET", "test.asp", true);
            xmlHttp.onReadyStateChange =function()
               {
                      if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                  alert("DynAjax: " + xmlHttp.responseText);
            }
               }
             xmlHttp.send();
}      
Avatar of Arsenico
Arsenico

Hi, try this.
The problem is
xmlHttp.onReadyStateChange instead of xmlHttp.onreadystatechange
and
handler(); instead of handler;

<script>
	function Ajax() {
		var xmlHttp = new XMLHttpRequest();
		
		xmlHttp.open("GET", "default.asp", false);
		xmlHttp.send();
		alert("Ajax:"  + xmlHttp.responseText);
	}

	function DynAjax() { 
		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = handler;
		/*xmlHttp.onreadystatechange = function() {
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					alert("DynAjax: " + this.responseText);
				}
			};*/
		xmlHttp.open("GET", "default.asp", true);
		xmlHttp.send();
	}	


	function handler() {
		if(this.readyState == 4 && this.status == 200) {
			alert("DynAjax: " + this.responseText);
		}
	}
</script>

Open in new window

or this

function DynAjax()
{
	var xmlHttp = new XMLHttpRequest();
            
        xmlHttp.open("GET", "test.asp", true);
        xmlHttp.onReadyStateChange =function()
        {
		handler(xmlHttp);
        }
        xmlHttp.send();
}      

function handler(xmlHttp)
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) 
	{
                  alert("DynAjax: " + xmlHttp.responseText);
        }
}

Open in new window

try this
xmlHttp.onreadystatechange = handler();
Avatar of Wassini

ASKER

Arsenico/ddsh79: If I change to onreadystatechange instead of onReadyStateChange, IE 8 reports: "Not implemented"

gurvinder372: This does nothing either! Same problem!
what is error? on which line?
this also?
function DynAjax()
{
	var xmlHttp = new XMLHttpRequest();
            
        xmlHttp.open("GET", "test.asp", true);
        xmlHttp.onReadyStateChange =function(xmlHttp)
        {
		handler(xmlHttp);
        }
        xmlHttp.send();
}      

function handler(xmlHttp)
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) 
	{
                  alert("DynAjax: " + xmlHttp.responseText);
        }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Arsenico
Arsenico

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wassini

ASKER

Strange! I have another PC where I can call the handler WITH parameters... But apparently not on this PC! (???)

But thanks!