Variable doesn’t get returned from AJAX function

You can’t do that : as the call is asynchronous, the get_data function can’t return the result of the ajax call.

What you should do is provide a callback to the get_data function and handle the result in the callback.

function get_data(data, destination, callback) 
         {

            if (lock_get == 0)
            {
                lock_get = 1;
                $.ajax({
                    type: "POST",
                    url: destination,
                    async: true,
                    data: data,
                    success: function(data) 
                    {
                        lock_get = 0;
                        if (data && callback)
                        {
                            callback(data);
                        }
                    }
                });
            }
         };

And call it like that :

get_data(data, destination, function(test){
   notice(test);
});

Leave a Comment