Loading gif image while jQuery ajax is running

后端 未结 4 885
北海茫月
北海茫月 2020-12-08 11:57

I am trying to show a loading image while my ajax call is running, however using the beforeSend attribute is not changing my result area.

$.ajax         


        
相关标签:
4条回答
  • 2020-12-08 12:09

    You are were missing a comma after cache: false

    0 讨论(0)
  • 2020-12-08 12:14

    You can also use the following:

    $('#tblLoading').ajaxStart(function() { $(this).show(); });
    $('#tblLoading').ajaxComplete(function() { $(this).hide(); });
    
    0 讨论(0)
  • 2020-12-08 12:25

    I had a similar problem. To resolve my issue, I replaced the .text, in the beforeSend section, with .html and created a html tag to insert into the element that holds my status. The success function did not replaced the content created by the .text() function but it did replace content created by the .html() function.

    $.ajax({
      url: "/answer_checker.php",
      global: false, type: "POST", 
      data: ({...clipped...}), 
      cache: false,
      beforeSend: function() {
        $('#response').html("<img src='/images/loading.gif' />");
      },
      success: function(html) {
        $('#response').html(html);
      }
    }
    
    0 讨论(0)
  • 2020-12-08 12:32

    I have a solution, it may not be the best way to do it but it has worked in this case.

    $('input').keyup(function () {
    
      $('#response').text('loading...');
    
      $.ajax({
        url: "/answer_checker.php",
        global: false, type: "POST", 
        data: ({...clipped...}), 
        cache: false,
        success: function(html) {
          $('#response').html(html);
        }
      });
    });
    

    By setting the resonce content before calling the ajax function it remains showing the loading text until the ajax call updates the same content.

    Thanks to everyone who took the time to answer.

    Alan

    0 讨论(0)
提交回复
热议问题