You need to either use a synchronous ajax request (not typical or recommended), and grab the data into a variable in the outer scope or figure out how to manipulate the data in the callback function. The problem is that the ajax function returns before the end of the ajax call - it is asynchronous.
Synchronous path:
function getCacheImage(direction) { var capture; jQuery.ajax({ url: json_request_string, aSync: false, success: function(data) { capture = data; } }); return capture; }
Typically using a callback
function getCacheImage(direction,callback) { jQuery.ajax({ url: json_request_string, success: function(data) { callback( data ); } }); } getCacheImage('left', function(data) { ...do something with the data... }
source share