Getting Server data using AJAX

B

bruce

I want to get access to some server data to be used by JavaScript. I
am using AJAX and can get the data using a client ID (<div
id=datahere></div). This works great. But, I want to use data that
ends up at "datahere" in a JavaScript function using

obj.innerHTML = xmlhttp.responseText;

Also, if I place a global of

var MyData;
and set it to
MyData = "TEMP";

Then modify the ajax routine to have

MyData = xmlhttp.responseText;
alert(MyData);

The alert does display my retrieved item but I still have "TEMP" in
MyData when I try to access it with my other function. I know it's the
'asynchronous nature of AJAX that's giving me the problem. So, what do
I need to do. I tried

<input type=hidden id=valueId value= "">

This didn't seem to work either.

Again, how to I get at the data returned from the server by AJAX in
another JavaScript function?

Thanks for the help...
 
S

Scott Sauyet

bruce said:
Again, how to I get at the data returned from the server by AJAX in
another JavaScript function?

You'll need to put the code that depends upon the changed value inside
a function that either serves as the callback handler for the AJAX
call or is called by that handler. Asynchronous communication
requires a somewhat different programming style.

// ...
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
handleChangedData(xmlhttp.responseText);
}
}
 
B

bruce

You'll need to put the code that depends upon the changed value inside
a function that either serves as the callback handler for the AJAX
call or is called by that handler.  Asynchronous communication
requires a somewhat different programming style.

    // ...
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        handleChangedData(xmlhttp.responseText);
      }
    }

Thanks for the response. Your suggestion changes my design (which is
okay) so I'll need to modify stuff and try it out. To quote Arnold,
"I'll be back." Thanks again..

Bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top