-1

I'm trying to create a reusable function where I could pass arg, and based on that execute ajax call. So far this is what I got:

ajaxFunc: function(url, method, args) {
    return {
        var URL = url,
            METHOD = method,
            ARGS = args,
            ERROR = error,
            index = -1,
            ajaxResponse,
            AJAX = function() {
                var url = URL + ((args == null) ? '' : ('/' + ARGS )),
                    data = (METHOD == 'post') ? ARGS[index] : undefined,
                    thisObject = this;

                $.ajax({
                    url:url,
                    method:METHOD,
                    data: data,
                    dataType: 'json',
                    error: function(jqXHR, textStatus, error) {
                        console.error(jqXHR, textStatus, error)
                    },
                    success: function(response) {
                        ajaxResponse = response;
                    }
                });

                return ajaxResponse;
            };

        AJAX();
    };
};

My problem is, how can i get the return to work? - when I call the function I can see the ajax getting call, but the function never return the response.

E.g: If I do MAIN.util.ajaxFunc('http://url.com/', 'get', null); I get nothing in return. I would like to find a way where I could make a ajaxFunc call and after that check for the response, if the response.length do something.

What I'm doing wrong?

Louis
  • 1,014
  • 2
  • 11
  • 20
  • Your code completes before the ajax receives a response. – Marko Jan 14 '14 at 22:51
  • 1
    Use a callback, just as jQuery does, and all of evented JavaScript. There’s no nice way to turn something inherently asynchronous synchronous, and your users will thank you for not doing it. – Ry- Jan 14 '14 at 22:52
  • `return { var URL = url` doesn't look right. – Kevin B Jan 14 '14 at 22:59

2 Answers2

1

The problem is that ajax is async. I would do something like this:

Use a deferred:

ajaxFunc: function (url, method, args) {
    //...

    return $.ajax({
        url: url,
        method: METHOD,
        data: data,
        dataType: 'json',
        error: function (jqXHR, textStatus, error) {
            console.error(jqXHR, textStatus, error)
        }
    });
};

MAIN.util.ajaxFunc('http://url.com/', 'get', null).done(function(response) {

});

Or use a callbacK:

ajaxFunc: function (url, method, args, callback) {
    //...

    return $.ajax({
        url: url,
        method: METHOD,
        data: data,
        dataType: 'json',
        error: function (jqXHR, textStatus, error) {
            console.error(jqXHR, textStatus, error)
        },
        success: function (response) {
            callback(response);
        }
    });
};

MAIN.util.ajaxFunc('http://url.com/', 'get', function(response) {

});
Jason P
  • 26,984
  • 3
  • 31
  • 45
0

With asynchronous operations like this, don't return the response. Instead, pass the data to another function that needs it. Any functions that chain from there will have to be a part of that new chain, like this:

success: function(response) {
  myFunction(response);
}

Rather than using a callback like that, you could instead use promises. That accomplishes the same thing, but avoids function nesting. It looks like this:

$.ajax({
    url: url,
    method: METHOD,
    data: data,
    dataType: 'json',
}).then(function (data) {
    myFunction(data);
});

Read more about promises here: http://api.jquery.com/category/deferred-object/

m59
  • 43,214
  • 14
  • 119
  • 136