return value from ajax request to another function

Posted by stormdrain on Stack Overflow See other posts from Stack Overflow or by stormdrain
Published on 2010-03-18T14:16:41Z Indexed on 2010/03/18 14:31 UTC
Read the original article Hit count: 429

Filed under:
|
|

I'm trying to return a value from (transport) to the calling function, but can't seem to get it to work:

function createXMLHttpRequest( w )
{
  var urly = '/users/notes/' + w;
  var options = {
      method:'get'
    , onSuccess: function( transport )
      {
        x = transport.responseText;
        return x;
      }
    , onFailure: function( transport )
      {
        var response = transport.responseText;
        alert( "FAILED "+ response );
      }
  };
  new Ajax.Request( urly, options );
  alert( x );
}

var ai = $( 'addItem' );
ai.onclick = function()
{
  // -1 indicates new
  addnote( -1, null );
}

x always alerts undefined. Unless I assign x to the Ajax.Request e.g. x=new Ajax.Request(urly,options). It then will alert [Object object]. How can I return the value of transport.responseText to the onclick function?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX