Saving AJAX responseText to a variable



Greetings!

I have a situation where I'm using AJAX to return some data, but
instead of displaying the data immediately on the page (as I seem to
only see in any example on the web; i.e., obj.innerHTML =
ajaxObject.responseText), I would like to store the responseText value
into a variable for further processing. My code is below:

====== Begin snippet ============
var ajaxObject;
var my_variable;

function getData(arg)
{
ajaxObject = GetXmlHttpObject(); // function from W3Schools

if(ajaxObject == null)
{
alert("We're sorry, but we are unable to retrieve the data because
your browser does not support AJAX.");
return;
}

var url = "getdata.php?arg=" + arg + "&rand=" + Math.random(); //
random number prevents same data due to caching without screwing
around with headers

ajaxObject.onreadystatechange = ajaxObjectStateChange;
ajaxObject.open("GET", url, true);
ajaxObject.send(null);

alert(my_variable); // always displays undefined
// do stuff with my_variable
}

function ajaxObjectStateChange()
{
if(ajaxObject.readyState == 4)
{
my_variable = ajaxObject.responseText;
}
}
============= End Snippet =============

As you can see, I use an alert to return the value I'm trying to
retrieve, but it always come up as undefined (or a empty string if I
initialize the variable). I've tried several different approaches
(that I won't post here for sake of brevity) and no matter what I try
I can never get the data to store in the variable. I'm sure there has
to be a way to do this, but as I've stated the only example I can ever
find on the web are the ones for immediately displaying the data.

Does anyone know of a way that I can store the responseText into a
variable for further processing, or even if it's possible?

Thanks.

~~tim

.



Relevant Pages

  • Re: Synchronizing the Main form and six (6) subforms?
    ... either just displaying the fields that relate the particular ShipType on the ... Am I to add this field to my one big table; and, how do I store ... >>> shipment to the correct main record. ...
    (microsoft.public.access.formscoding)
  • Re: combo box columns and key input
    ... Do not store the text along w/ the number in the table. ... The TextBox is an unbound control ... and if you hit 0 the ComboBox displayed None ... So you are displaying 0 - max num in the combo box ...
    (comp.databases.ms-access)
  • Re: DefaultTableModel() is a crap / How to hide a Table column?
    ... Mark Sizzler wrote: ... > It all started with my search for a possibility to store data values in a JTable without displaying it. ... You said "What a poor system architecture." ...
    (comp.lang.java.gui)
  • Re: Storing Database Result Field in a Variable.
    ... I have an ASP page that displays records from a SQL database, ... displaying all the records. ... not able to store the value to a variable. ... AND CostType = 'MT114')) AS Total, ...
    (microsoft.public.frontpage.programming)