weixin_33724059 2015-10-20 07:11 采纳率: 0%
浏览 57

使用Ajax加载图标

I am trying to show ajax-loader.gif while data is loading, and after data is loaded completely, hide it.

Code update:

 $.ajax({
type: "POST",
dataType: 'json',
url: "api/Employee/GetData",
beforeSend: function () {
    // before send, show the loading gif
    $('#wait').show();
},
success: function (msg) {
    $.getJSON(uri).done(function (data) {

        $.each(msg, function (key, item) {
            // alert(item);
            $('<tr>', { html: formatItem(item) }).appendTo($("#tbdata"));
        });
    });
    /* or simply put here each statement inside callback like so          
     $.each( msg, function (key, item) {
      // alert(item);
      $('<tr>', { html: formatItem(item) }).appendTo($("#tbdata"));
     });
    */
    // or just hide here on success
    $('#wait').hide();
},
complete: function () {
    // or hide here
    // this callback called either success or failed
    $('#wait').hide();
}
}).done(function (data) {
$.each(data, function (key, item) {
    // alert(item);
    $('<tr>', { html: formatItem(item) }).appendTo($("#tbdata"));
});
});

According to some answers, Ajax has start and stop functions as shown here:

$('#wait').ajaxStart(function() {
$(this).show();
}).ajaxComplete(function() {
$(this).hide();
});

My question is how to combine both codes in order to show and hide ajax-loader.gif?

  • 写回答

1条回答 默认 最新

  • 程序go 2015-10-20 07:19
    关注

    ajaxStart and ajaxComplete should be attached on document. Must be noted that, any ajax call will affected if you are using the global start and complete like you did. Anyway you can do like this :

      $(document).ajaxStart(function() {
         $('#wait').show();
      });
      $(document).ajaxComplete(function() {
        $('#wait').hide();
      });
    

    Other than that you can use beforeSend and complete AJAX setting properties , IMHO latter code much more prefer than former since the latter code affected to it request only while former affected another ajax request if you have more than that :

    $.ajax({
        type: "POST",
        dataType: 'json', 
        url: "api/Employee/GetData",
        beforeSend : function () {  
           // before send, show the loading gif
           $('#wait').show(); 
        },
        success: function ( msg ) { 
          /* or simply put here each statement inside callback like so          
           $.each( msg, function (key, item) {
            // alert(item);
            $('<tr>', { html: formatItem(item) }).appendTo($("#tbdata"));
           });
          */ 
          // or just hide here on success
           $('#wait').hide();
        },
        complete : function () { 
         // or hide here
         // this callback called either success or failed
         $('#wait').hide();
        }
    }).done(function (data) {
        $.each(data, function (key, item) {
           // alert(item);
            $('<tr>', { html: formatItem(item) }).appendTo($("#tbdata"));
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度