Forum Moderators: open

Message Too Old, No Replies

undifine the return value from the AJAX

undifine the return value from the AJAX

         

xbl01234

5:52 am on Oct 21, 2007 (gmt 0)

10+ Year Member



Hi;
I am try to use the xmlHttp.responseText's value, but i find it some time works, some time does not work.

in my code, i got some buttons as < 1 2 3 >.
after i click the above buttons, some times it pop up a window
with undifine
, some times it pop up a window with the right value. This is my code's problem, could any one help me to solve this problem, please.

index.html


<html>
<body>

<input type="button" id="b<" name="bb<" value="<" onClick="cButtons()">
<input type="button" id="b1" name="bb1" value="1" onClick="cButtons()">
<input type="button" id="b2" name="bb2" value="2" onClick="cButtons()">
<input type="button" id="b3" name="bb3" value="3" onClick="cButtons()">
<input type="button" id="b>" name="bb>" value=">" onClick="cButtons()">

<script type="text/javascript">
var rValue;

function cButtons(){
ajaxFunction();
alert(rValue);
}

function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
rValue=xmlHttp.responseText;

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

</script>

</body>
</html>

cateV.php


<?php
$test="i am testing";
echo $test;
?>

mehh

10:38 am on Oct 21, 2007 (gmt 0)

10+ Year Member



replace
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
rValue=xmlHttp.responseText;

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


with:

xmlHttp.open("GET","cateV.php",false);
xmlHttp.send(null);
rValue=xmlHttp.responseText;

Untested, but should work

xbl01234

10:57 am on Oct 21, 2007 (gmt 0)

10+ Year Member



Thanks a lot, it works now, but why the change make it different?

mehh

12:03 pm on Oct 21, 2007 (gmt 0)

10+ Year Member



You were sending an asynchronous request and you wanted a synchronous request. So what was happening was you where setting rValue after you alerted it. The new code works like this
xmlHttp.open("GET","cateV.php",false);//set a synchronous request.
xmlHttp.send(null); //All scripting on the page will now stop until cateV.php is got
rValue=xmlHttp.responseText;//set the text as your value. Seeing as we know we have the page there is no need to use onreadystatechange

xbl01234

10:23 am on Oct 22, 2007 (gmt 0)

10+ Year Member



I got some questions to ask you:

1)


xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
rValue=xmlHttp.responseText;

}
}

The above means the browser send requests to server one by one, and it receives data from server also one by one, Am I right?


rValue=xmlHttp.responseText;

without the xmlHttp.onreadystatechange=function(), does it means the browser can send many requests to server at the same time, and it also
can receive many data from server at the same time as well.

2)


xmlHttp.open("GET","cateV.php",false);
xmlHttp.send(null);
rValue=xmlHttp.responseText;

why you put the "rValue=xmlHttp.responseText;" in the last position, not on the top of them?