Solved

Ajax onReadyStateChange problem

Posted on 2010-09-14
8
598 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

828 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