Jump to content

Xmlhttp.readystate == 4


GerryH

Recommended Posts

I was hoping one of you script guru's could help me. I have a page where if the user clicks a button, it triggers an AJAX call that retrieves data from my database. The problem is it doesn't always return the modified string. I tried to add a setTimeout() but that's futile. Is there a method where my code won't run asynchronously until the readyState == 4?P.S. I tried a while loop, but it never terminated.

//////////////////////////////////////////////////////////////////////////////////////////////////////// Load Page is an AJAX function called when a user selects a button and retrieves the body of the <div> element.// This calls the changecontent.php script on the server to retrieve the html text from the database.//////////////////////////////////////////////////////////////////////////////////////////////////////function LoadAJAXPage(str){ 	xmlHttp=GetXmlHttpObject();	if (xmlHttp==null)	{	   	alert ("Sorry, your browser does not support AJAX!");		  return;	}	var url="changecontent.php";	url=url+"?q="+str;	url=url+"&sid="+Math.random();	// Create a delay to allow the server to respond (doesn't work :( )	var x = setTimeout("doNothing()", 2000);		xmlHttp.onreadystatechange=stateChanged;	clearTimeout(x);	xmlHttp.open("GET",url,true);	xmlHttp.send(null);}function doNothing(){}//////////////////////////////////////////////////////////////////////////////////////////////////////// Return the string from the database //////////////////////////////////////////////////////////////////////////////////////////////////////function stateChanged(){ 	  if (xmlHttp.readyState==4)	 	string1=xmlHttp.responseText;}

Thanks in advance!

Link to comment
Share on other sites

In other words, forget the timeout/wait concept. You're going to open your AJAX object, and then send it. When the response comes back, the browser will load it into AJAX object.Now all this time, the browser has been checking for that response, and updating the readystate of your AJAX object. That's why you're waiting for it to equal 4. That means the response has arrived.You've followed normal practice and set up a series of statements to be executed when the readystate does equal 4. Those statements are the second half of the AJAX operation. Your LoadAJAXPage() function is the first half. Your stateChanged() function is the second half. It seems like an unusual programming concept, but it really isn't. Think of it as just another event handler. If you have a button set up with onclick="myfunction()", you know that myfunction() is just sitting there, doing nothing, waiting for a click event to trigger it. Same thing with your stateChanged() function. When the response comes back, it gets triggered.----Now, you say the string doesn't always come back modified. By any chance is your triggering button in a form? And is the button a submit button? This leads to a common error. If you don't capture the submit event, then your AJAX message will get sent, but your form will get sent also, and your page will reload, so your AJAX object will get destroyed.

Link to comment
Share on other sites

...Now, you say the string doesn't always come back modified. By any chance is your triggering button in a form? And is the button a submit button? This leads to a common error. If you don't capture the submit event, then your AJAX message will get sent, but your form will get sent also, and your page will reload, so your AJAX object will get destroyed.
Thanks for the quick response, no I'm not using the traditional form or submit elements, just an <img> with an onclick handler that triggers the AJAX function. I modified my "stateChanged()"
function stateChanged(){ 	  if (xmlHttp.readyState==4)		 string1=xmlHttp.responseText;	  else		string1=xmlHttp.readyState;}

And this is a very "hit-and-miss" thing (no pattern whatsoever) I will randomly return the string1 as "1", which I believe is the "The request has been set up" state. Of course if I remove the else clause, the string1 var never gets updated.Thanks again!

Link to comment
Share on other sites

The variable string1 is local to the function. That means that it cannot be used outside of the function.You should try this instead:

if (xmlHttp.readyState==4) {  string1=xmlHttp.responseText;  doSomething(string1);}

And then make the function like this:

function doSomething(param) {  alert(param);}

Link to comment
Share on other sites

if I remove the else clause, the string1 var never gets updated.
Are you sure the server side is working? Easiest way to check in your case is to type the url+query string directly into your location bar and see what gets output. Make sure it's exactly the same url+query as your script is generating.
Link to comment
Share on other sites

Are you sure the server side is working? Easiest way to check in your case is to type the url+query string directly into your location bar and see what gets output. Make sure it's exactly the same url+query as your script is generating.
Sorry, I meant to say if I remove the else statement from my stateChanged(), I will randomly get the output from my php script. And when I load the fat url directly in any brower, it returns 100% of the time, where the ajax averages 70-80% success.One other question, I realise my stateChanged() is an event handler, but what confuses me is why (looking at the code proceduraly) I'm checking for a ready state (which returns the output of my php script) before I send the request to the server?Thanks again!
Link to comment
Share on other sites

Sorry, I meant to say if I remove the else statement from my stateChanged(), I will randomly get the output from my php script. And when I load the fat url directly in any brower, it returns 100% of the time, where the ajax averages 70-80% success.One other question, I realise my stateChanged() is an event handler, but what confuses me is why (looking at the code proceduraly) I'm checking for a ready state (which returns the output of my php script) before I send the request to the server?Thanks again!
You're not checking the ready state before, you're assigning the event listener before.For the same reason you assign an onclick event before the user clicks, you assign the onreadystatechange event before waiting for the response from the server.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...