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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.