PDA

View Full Version : undifine the return value from the AJAX



kamkam
Oct 21st, 2007, 06:40 AM
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;
?>

rwedge
Oct 21st, 2007, 09:39 AM
Try:
xmlHttp.open("GET","cateV.php",false);

kamkam
Oct 21st, 2007, 10:16 AM
Try:
xmlHttp.open("GET","cateV.php",false);

it will pop up a window with undefine all the time, when i click the buttons.

but if i declare a local varibal for that, it is not problem. as flollowing:


xmlHttp.onreadystatechange=function()
{

if(xmlHttp.readyState==4)
{
var myV;
myV=xmlHttp.responseText;
alert(myV);

}
}


but for the global varibal, it does not work properly, sometimes it pop up a window with value, sometimes it pop up a window with undefine.

rwedge
Oct 21st, 2007, 10:21 PM
var rValue;
function cButtons(){
ajaxFunction();
alert(rValue);
}
When the function cButtons() is called, with async set to true and the var rValue initalize as undefined, the alert(rValue) is executed reguardless of the ajaxFunction() being completed or not. With async set to false javascript is blocked until the request is completed.

Set to true allows a better user experience as discussed here (http://yuiblog.com/blog/2006/04/04/synchronous-v-asynchronous/), but with your function cButtons() as it is, true can cause at least the first alert to be undefined.

When you join the alert with the response
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
var myV;
myV=xmlHttp.responseText;
alert(myV);
}
}it is executed only when the response is completed.

kamkam
Oct 22nd, 2007, 11:28 AM
Thanks a lot, let me have some time to understand what you say.