I'm using this type of sending data through jQuery.post, where do I put a loading image... or a message while the function returns something?
$.post( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
});
I'm using this type of sending data through jQuery.post, where do I put a loading image... or a message while the function returns something?
$.post( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
});
The trick is to use ajaxStart and ajaxStop .
These events are called by jQuery every time an ajax request starts/ends. Whether they are done by the methods
post
,ajax
,load
, ..It is very useful because if there are several ajax requests in parallel, the event
ajaxStop
is called when the last request finishes. And the eventajaxStart
is not repeated if there is already a request in progress.In the script that you propose, the function is a callback and it is executed when an event occurs (in this case, when the post ends), as you want to do it at that moment, it is enough to act outside the callback.
If what you need is to do this for any AJAX, @rnd's answer is better.