Solved

Ajax onReadyStateChange problem

Posted on 2010-09-14
8
597 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
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)

778 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