Jump to content

Ajax only seems to understand "readyState" if I alert them.


Thetcm

Recommended Posts

I'll just start by saying that the thing I'm working on is highly dynamic and has a lot of Ajax requests possible to get or send data to a database, and all my scripts work properly except for this one, even though it's exactly like the rest.

 

function setSort(sor){

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	return;
	}

var url="setSort.php?sor="+sor;

xmlHttp.onreadystatechange=function(){
	//alert(xmlHttp.readyState);
	if(xmlHttp.readyState==4){
	  
	  alert(xmlHttp.responseText);
	  
	}

}
xmlHttp.open("GET",url,true);
xmlHttp.send(null)

}

 

GetXmlHttpObject() is defined somewhere else and isn't the problem. As you can see, it's pretty straight foward. I send a request to setSort.php and I try to show the result in a alert box(that's not what I'm doing, but since it didn't work I tried this).

 

The PHP is pretty standard too :

 

<?php

session_name(SESSION_NAME);
session_start();

if(isset($_GET['sor'])) {

	$_SESSION['sort'] = $_GET['sor'];

}

echo $_SESSION['sort'];

?>

 

I'm not even doing anything here, aside declaring a $_SESSION variable to be used in my call to show my list, in order to sort it properly. It couldn't be simpler.

 

"sor", the data passed around, is actually the ID of the TD I clicked on. Standard stuff again.

 

But the

 

xmlHttp.onreadystatechange=function(){
	//alert(xmlHttp.readyState);
	if(xmlHttp.readyState==4){
	  
	  alert(xmlHttp.responseText);
	  
	}

}

 

That way it's not doing anything. BUT if I remove the comments of the "//alert(xmlHttp.readyState);" line, the script seems to get that, yes, in fact, readyState did become 4 at some point, and does what it's supposed to.

 

What's wrong?

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.