Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I use jquery ajaxstart and ajaxstop with $.post?

Tags:

jquery

ajax

Basically I want to show a loading gif...

here's the code I'm using:

$("#mail-change input[type=submit]").click(function(event){

$.post('user_settings.php', $("#mail-change").serialize(), function(res) {

$(res).insertBefore(".grey");

}, 'html')

});
like image 598
pete Avatar asked Nov 22 '10 23:11

pete


People also ask

What is ajaxStart in jQuery?

The ajaxStart() method specifies a function to be run when an AJAX request starts. Note: As of jQuery version 1.8, this method should only be attached to document.

How would you fire a callback when any AJAX request on a page has completed?

The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.

What is ajaxSetup?

Definition and Usage. The ajaxSetup() method sets default values for future AJAX requests.

What is AJAX event?

The ajaxStart and ajaxStop events are events that relate to all Ajax requests together. ajaxStart (Global Event) This event is triggered if an Ajax request is started and no other Ajax requests are currently running.


1 Answers

$("#loading").ajaxStart(function() {
    $(this).show();
}).ajaxStop(function() {
    $(this).hide();
});

EDIT:

$("#mail-change input[type=submit]").click(function(event){
    $("#loading").show()
    $.post('user_settings.php', $("#mail-change").serialize(), function(res) {
        $(res).insertBefore(".grey");
        $("#loading").hide();
    }, 'html');
});

or:

$.ajax({
   url : 'user_settings.php',
   data: $("#mail-change").serialize(),
   beforeSend: function(){
     $("#loading").show();
   },
   complete: function(){
     $("#loading").hide();
   },
   success: function(res) {
     $(res).insertBefore(".grey");
   }
 });

See:

  • http://api.jquery.com/ajaxStart/
  • http://api.jquery.com/ajaxStop/
  • http://docs.jquery.com/Ajax_Events
  • http://api.jquery.com/category/ajax/global-ajax-event-handlers/
like image 188
karim79 Avatar answered Nov 01 '22 11:11

karim79