Solved

Ajax onReadyStateChange problem

Posted on 2010-09-14
8
606 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:Wassini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33670612
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();
}      
0
 

Expert Comment

by:Arsenico
ID: 33670616
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

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33670625
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

0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33670640
try this
xmlHttp.onreadystatechange = handler();
0
 

Author Comment

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

gurvinder372: This does nothing either! Same problem!
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33670798
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

0
 

Accepted Solution

by:
Arsenico earned 125 total points
ID: 33670822
Try the code I posted,
i tested it on IE 8 and it works correnctly.
The problem is the parentesi
try this
xmlHttp.onreadystatechange = handler;
not handler()
0
 

Author Comment

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

But thanks!
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question