Saving AJAX responseText to a variable
- From: tempacct@xxxxxxxxxx
- Date: Sat, 21 Jul 2007 12:14:41 -0700
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
.
- Follow-Ups:
- Re: Saving AJAX responseText to a variable
- From: Phlip
- Re: Saving AJAX responseText to a variable
- From: Ant
- Re: Saving AJAX responseText to a variable
- Prev by Date: Re: c regex example
- Next by Date: Re: c regex example
- Previous by thread: Free test papers of companies,GRE,GMAT
- Next by thread: Re: Saving AJAX responseText to a variable
- Index(es):
Relevant Pages
|