Solved

Ajax onReadyStateChange problem

Posted on 2010-09-14
8
594 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
 
LVL 14

Expert Comment

by:ddsh79
ID: 33670640
try this
xmlHttp.onreadystatechange = handler();
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now