Forum Moderators: open

Message Too Old, No Replies

AJAX responseText

How can I clear the stored value of responseText?

         

bpejman

11:11 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



Hi Everyone,

This may be weird but I can't seem to figure out why responseText is not returning the value my PHP code returns.

So here it is: I have an AJAX enabled script that simply calls a PHP file called test.php. All test.php does is returns a -1.

Javascript:


httpobj.open('get', 'test.php', true);
httpobj.setRequestHeader("Connection", "close");
httpobj.send(null);
httpobj.onreadystatechange = function()
{
if(httpobj.readyState == 4 && httpobj.status == 200)
{
alert(httpobj.responseText);
}
}

PHP:

<?
echo -1;
?>

When I receive an alert, the value of httpobj.responseText is "0-1". To me, it seems that responseText is holding on to previous values. Does anyone know how I can clear/reset the value so only -1 is returned?

Thanks for any input.

Bobby Pejman

bpejman

12:46 am on Jul 14, 2009 (gmt 0)

10+ Year Member



AAArrrrgggg! Ok I figured it out as stupid as it may seem. In my PHP I used to use "return -1" but AJAX and responseText does not understand returns from the server-side but rather the response the server-side script writes. I then changed the returns to echos so I had:

if (x)
echo 0
if (y)
echo -1

It turned out that both conditions x & y were met which made me get an error of 0-1. I ended up doing the following:

if (x)
echo 0;
return;
if (y)
echo -1;
return;

Anyway, I hope this helps someone out although it's a stupid mistake.