0

Possible Duplicate:
Return Value from inside of $.ajax() function

Take the following function for example:

function($data) {
    $.post(
        url,
        {
            'data': $data
        },
        function(response){
            return response;
        }
    );
}

How could I make the parent function: function($data) { ... } return response?

I am unable to put my the rest of my logic into the post callback due to the nature of my script. (see below)

var methods = {

        'email' : function(field) {
            var value = field.val();
            var response = false;
            field.addClass("loading");
            $.post(
               ajaxData.url, 
               {
                  'action':'validate_form',
                  'value': value,
                  'method': field.data('method')
               }, 
               function(response){
                   return response;
               }
            ).complete(function() {
                field.removeClass("loading");
            });
        },

        'password' : function(field) {
            var value = field.val();
            var response = {};
            if (value.length < 8) {
                response.result = false;
                response.message = 'Your password must be a minimum of 8 characters';
            } else {
                response.result = true;
                response.message = false;
            }
            return response;
        },

        'verify_password' : function(field, dependancies) {
            var value = field.val();
            var response = {};
            if (value != dependancies["password"].val()) {
                if (!dependancies["password"].val() || !value) {
                    return false;
                }
                response.result = false;
                response.message = 'Passwords do no match';
            } else {
                response.result = true;
                response.message = false;
            }
            return response;
        }
    }

Where each property of methods is a function which may be called and the return value is used later in the script.

var response = methods[field.data('method')](field, field.data('dependancies'));
Community
  • 1
  • 1
George Reith
  • 13,132
  • 18
  • 79
  • 148
  • 2
    The nature of asynchronous callback-based programming is such that you have no choice but to do the work from the callbacks. You cannot have a return value as you describe it, in other words. Perhaps you can wrap function calls to your methods in other functions, or something like that. – Pointy Jul 18 '12 at 15:23

1 Answers1

2

As A of AJAX stands for Asynchronous, so you can't return like you want. You can try with a callback function like below:

function($data, callback) {
    $.post(
        url,
        {
            'data': $data
        },
        function(response){
            return callback(response);
        }
    );
}

For example:

var myObj = {
   myfunc: function($data, callback) {
        $.post(
            url,
            {
                'data': $data
            },
            function(response){
                return callback(response);
            }
        );
    }
}

myObj.myfunc($data, function(response) {
  // process here with response
});
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164