return value from ajax request to another function


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?


Your Request is not asynchronous:false. So x is alerted before the request is completed and x is set to transport.responseText.

You have to either set the request as synchronous, or alert x in the onSuccess method.

The return value from onSuccess is discarded by Ajax.Request. Don't return anything.

Ajax is asynchronous by nature. You should query the server, then handle the response in a callback. So you should not try to use the server value in the onclick function, but rather in a separate callback (the onSuccess method).


This is just wrong!

When doing ajax calls you should forget to think synchronous, think asynchronous instead! Send the request to some url and send a callback function that will be executed once request returns some data.

I wrote simple wrapper for GET calls

// AJAX WRAPPER MYPROJECT = {}; MYPROJECT.Ajax = (function() { return{ // get data by AJAX call and return as plain text get:function(url, callback) { new Ajax.Request(url, { method: 'get', onSuccess: function(transport) { var data = transport.responseText.evalJSON(); callback(data); } }); } } })(); // USAGE MYPROJECT.Ajax.get('/ajax_json_echo/?foo=bar',function(data){ alert(data); $('containerText').innerHTML = data.get_response.foo });

You can see the code in action here <a href="http://jsfiddle.net/wbDhY/1/" rel="nofollow">http://jsfiddle.net/wbDhY/1/</a>

(please be patient, the alert box should appear in 2 seconds)


  • how set images on buttons of navigation bar?
  • custom viewcontroller delegate not working
  • How can I run my development action on my Google Home?
  • Run-time Error 424 Object Required UserForm doesnt exist
  • Prevent duplicates from adding items from Listbox1 to Listbox2 (VBA excel)
  • Pandas: remove duplicates that exist in any order
  • jQuery unload with an AJAX call not working in Chrome
  • Excel Not Responding During Macro
  • How to integrate a custom GraphicsItem into a QML scene?
  • Service Fabric - how to gracefully fail an app
  • Create unique ids for a group
  • How to correctly append dynamic GetUIKit accordions?
  • Fail SonarQube quality gate when coverage decreases
  • KornShell Printf - Padding a string
  • Send data from edittext to listview
  • Mocha throws unexpected token error for ES6 object spread operator
  • Criterion causing memory consumption to explode, no CAFs in sight
  • Jquery Mobile pageLoading() Method how does it work?
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Simulate click Geckofx vb,net
  • Assign variable to the value in HTML
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Accessing IRQ description array within a module and displaying action names
  • Javascript Callbacks with Object constructor
  • Where to put my custom functions in Wordpress?
  • Redux, normalised entities and lodash merge
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • LevelDB C iterator
  • Is it possible to post an object from jquery to bottle.py?
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically