Prevent ajax call from firing twice

后端 未结 5 935
遥遥无期
遥遥无期 2020-12-05 20:20

I have an ajax call

$(\'#button1\').on(\'click\', function(e){
    $.ajax({
      url:  url,
      type: \'POST\',
      async: true,
      dataType: \'json\         


        
5条回答
  •  半阙折子戏
    2020-12-05 20:33

    As per the answer by Brennan,

    $('#button1').on('click', function(e){
        $('#button1').attr('disabled', 'disabled');
        $.ajax({
          url:  url,
          type: 'POST',
          async: true,
          dataType: 'json',
          enctype: 'multipart/form-data',
          cache: false,
          success: function(data){
             $('#button1').removeAttr('disabled');
          },
          error: function(){}
        });
        e.stopImmediatePropagation();
        return false;
    });
    

    Here the button will be disabled and will be enabled on success

提交回复
热议问题