dongye1912 2012-05-09 04:45
浏览 36

如何在使用jquery ajax时立即显示加载消息?

Below is my Ajax code I have made using jQuery Ajax method.

$(".loading_msg").show();
$.ajax({
    type: "POST",
    url: "submit_ops.php?action=submit_form",
    data: $( "#frmMyForm" ).serialize(),
    success: function(result){
        if(result=='Success'){
            $("#msgContainer").html('<div style="color:#3CA322;font-weight:bold;font-size:14px;padding:10px;">Thank you.</div>');
            $("#msgContainer").fadeIn();
        }
        else{
            $("#msgContainer").html('<div style="color:red;font-size:12px;padding:10px;">' + result + '</div>');
            $("#msgContainer").fadeIn();
        }
    },
    error: function(){
        //console.log('error');
    },
    complete: function(){
        //console.log('complete');
        $(".loading_msg").hide();
        $("#submit").attr("disabled", false);
    }

});

So the logic is, whenever the Ajax request is about to made,

  1. the .login_msg container is displayed first

  2. then ajax call made

  3. and when it is completed, again the .loading_msg is hidden.

Now the problem, it is not working smoothly as it should be. So when the ajax call is made, the browser hangs for a seconds but it doesn't show the loading message. But when the request is completed, the login message appears like for half a second and then it goes away as I have set it hidden on request completion.

So I want that the .login_msg should be shown before the request is made.

Any suggestions for this? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongxietao0263 2012-05-09 04:50
    关注

    try using beforeSend and complete settings of $.ajax:

    $.ajax({
        type: "POST",
        url: "submit_ops.php?action=submit_form",
        data: $( "#frmMyForm" ).serialize(),
        beforeSend:function(){
                 $(".loading_msg").show();
            }
        complete:function(){
                 $(".loading_msg").hide();
            }
    
        ....
        ....
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看