I've been searching for answers for a while, before thinking of asking here, but I just got stuck.
I've read some articles/question similar to mine, but ended following these one:
- JQuery - Storing ajax response into global variable // Answer: https://stackoverflow.com/a/905324
- jQuery: Return data after ajax call success
And I ended up with this:
var del_act = (function(){
var resp;
$.post(url+'delete/'+id, 'json')
.done(function(data) {
custom_alert(type.toUpperCase()+' deleted succesfully!');
if(redirect)
window.location.replace(url);
resp = true;
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
if(jqXHR['status'] == 404)
custom_alert(type + ' not found');
resp = false;
});
return {getResp: function()
{
console.log(resp);
return resp;
}};
})();
console.log(del_act.getResp());
But the problem is that both console.log
s returns an undefined
response. I know I could use $.ajax()
with async: false,
but I don't want to, so I'm trying to find a way of returning a true
or a false
response.