Panagiotis Koursaris Panagiotis Koursaris - 4 months ago 10
jQuery Question

AJAX done() executed when finished all of request staff

I have an AJAX request to let the user delete some data from the list. When the ajax done I call another ajax request so the list updated with new the list of data. AJAX request to delete data:

method: "DELETE",
url: urlRequest,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}).done(function() {
// the follow function call an ajax with get request so as the list will be updated with the new one
}).fail(function() {

The delete request above has a lot of information to delete, some text and some images and I am worried if
will be called before the server finishes all of this stuff.

My question: Is the
method safe for the above situation? If not, is there any other jQuery method that will be executed only if the request finishes all of its stuff?


Yes, it's entirely safe.

The done() handler will not be called until the promise returned by the $.ajax method has been resolved, which will only happen after the server has finished processing and returns a 200 OK response. If any other response is received then the fail() handler will execute instead.

In both cases the server processing will have stopped before a response is received by the client.