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?