Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ajax onReadyStateChange problem

Posted on 2010-09-14
8
Medium Priority
?
613 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Gurvinder Pal Singh
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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
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…
Suggested Courses

783 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