Ajax async with beforesend
I wanna call Ajax and wait til it finish to response data, and also before Ajax sends request, I wanna do some actions; but it does not seem to work, any solutions please?
Show what you have currently
@Sinnbeck here is my code:
$.ajax({
url: url,
type: 'delete',
data: {
_token: "{{csrf_token()}}"
},
async:false,
beforeSend:function(){
spinner.show();
},
success: function(response) {
spinner.hide();
notify(response.status, response.msg);
table.ajax.reload( null, false );
}
});
but spinner shows only after Ajax responds
@Saren Um Just do it on the line before the ajax?
spinner.show(); //here
$.ajax({
url: url,
type: 'delete',
data: {
_token: "{{csrf_token()}}"
},
async:false,
success: function(response) {
spinner.hide();
notify(response.status, response.msg);
table.ajax.reload( null, false );
}
});
@Sinnbeck I don't know why it's still not working, Ajax keeps executing first and spinner shows after
@Saren Um beforeSend() is for changing the request, so I dont know how it reacts if you run arbitrary code in it.
Maybe try returning true to ensure it is run
beforeSend:function(){
spinner.show();
return true;
},
@Sinnbeck still it is not working
@Saren Um but did my first example work?
@Sinnbeck Ajax always starts first. Now I put Ajax in setTimeout just to delay and it helps
@Saren Um Is that spinner.show(); slow? I assumed it just added something to the DOM, which should be so fast it would be done before the ajax has time to even start
Please or to participate in this conversation.