Link to home
Start Free TrialLog in
Avatar of tmonteit
tmonteitFlag for Afghanistan

asked on

Prototype Library Question

I have a JS prototype function that calls a servlet looking for the value in a key-value pair.  

   GetValue: function(key){
     debugAPI("GetValue: " + key);
     var myAjax = new Ajax.Request(
     '/TRIAD/api.do?dispatch=getValue&key=' + key,
     {
        method: 'get',
        onComplete: function(response) {$F("result") = response.responseText;}
     });
   },   ...

The servlet puts the value in a 'request.setAttribute("value", value); ' method.

How do I get that value back into a Javascript variable as the return value for the function?

as in:

var myValue = getValue(key);
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

The script you are sending the request to needs to return html output to the browser.  That text is then dumped into the response.responseText variable.

As an example:
http://72.34.47.183/~irlazy/ajax.html


<script src="prototype-1.4.0.js"></script>
<script>
function clickMe() {
      var myAjax = new Ajax.Request( 'test.html', { method: 'get', onComplete: showResponse });
}
function showResponse(originalRequest) {
        alert(originalRequest.responseText);
}
</script>

<input type=button value="Click Me" onClick=clickMe()>


The text that is returned by test.html is displayed in the alert box.
Hi,

Try looking into these functions...

function createRequestObject() {
     var ro;
     var browser = navigator.appName;
     if(browser == "Microsoft Internet Explorer"){
          ro = new ActiveXObject("Microsoft.XMLHTTP");
     }else{
          ro = new XMLHttpRequest();
     }
     return ro;
}

var http = createRequestObject();

function sndReq(action) {
     http.open('get', 'rpc.php?action='+action);
     http.onreadystatechange = handleResponse;
     http.send(null);
}

function handleResponse() {
     if(http.readyState == 4){
          var response = http.responseText;
          var update = new Array();

          if(response.indexOf('|' != -1)) {
               update = response.split('|');
               document.getElementById(update[0]).innerHTML = update[1];
          }
     }
}

R.K
Avatar of tmonteit

ASKER

Moroclaven,

So far, your example makes the most sense.  Still confusing is at the end state you have the variable within an embeded function 'showResponse'.  

I want to make the variable the return value of clickMe.  

How do I pass it from the embeded function to the clickMe function so it can be returned?

Thanx...
The key to this question is "RETURN VALUE".
ASKER CERTIFIED SOLUTION
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Note this solution was exactly on target.

https://www.experts-exchange.com/questions/22062473/Need-Syncronous-AJAX-Call.html

Moroclaven's answer here seems to work.  It is a good example on how to use XMLHttp.