当使用Ajax提交时显示加载图片

30

我知道互联网上有成千上万的示例,但我想让我已经拥有的脚本在检索数据时显示一个加载gif图像。我的Java知识很差,因此我想问如何更改以下内容:

<script type="text/javascript"> 
 $(document).ready(function(){
  function getData(p){
    var page=p;
    $.ajax({
        url: "loadData.php?id=<? echo $id; ?>",
        type: "POST",
        cache: false,
        data: "&page="+ page,
        success : function(html){
            $(".content").html(html);
        }
    });
}
getData(1);

$(".page").live("click", function(){
    var id = $(this).attr("class");
    getData(id.substr(8));
        });
      });
  </script> 

这是我的div:

    <div class="content" id="data"></div>

谢谢。 约翰

7个回答

62

假设页面上有一个标签,其中包含您的加载信息:

<div id='loadingmessage' style='display:none'>
  <img src='loadinggraphic.gif'/>
</div>

你可以在你的ajax调用中添加两行代码:

function getData(p){
    var page=p;
    $('#loadingmessage').show();  // show the loading message.
    $.ajax({
        url: "loadData.php?id=<? echo $id; ?>",
        type: "POST",
        cache: false,
        data: "&page="+ page,
        success : function(html){
            $(".content").html(html);
            $('#loadingmessage').hide(); // hide the loading message
        }
    });

5
如果 Ajax 请求不成功,则此情况下加载中的 GIF 图像仍然可见。 - ragklaat
1
不要在成功回调中隐藏div/gif,而是将其添加到always()方法下面(这是旧的complete()回调处理程序)。 - Narayana

10

Felipe,感谢您的建议。但是,我忘了提到我的js知识很差?我看过那些页面,但不知道如何更改我的脚本。 - user301017

9
$.ajax(
{
    type: 'post',
    url: 'mail.php',
    data: form.serialize(),
    beforeSend: function()
    {
        $('.content').html('loading...');
    },
    success: function(data)
    {
        $('.content').html(data);
    },
    error: function()
    {
        $('.content').html('error');
    }
});

玩得愉快!

如果您的网页加载速度很快,无法显示加载过程,您可以添加某种超时机制。


2
这非常简单,易于管理。
jQuery(document).ready(function(){
jQuery("#search").click(function(){
    jQuery("#loader").show("slow");
    jQuery("#response_result").hide("slow");
    jQuery.post(siteurl+"/ajax.php?q="passyourdata, function(response){
        setTimeout("finishAjax('response_result', '"+escape(response)+"')", 850);
            });
});

})
function finishAjax(id,response){ 
      jQuery("#loader").hide("slow");   
      jQuery('#response_result').html(unescape(response));
      jQuery("#"+id).show("slow");      
      return true;
}

1
<div id="load" style="display:none"><img src="ajax-loader.gif"/></div>

function getData(p){
        var page=p;
        document.getElementById("load").style.display = "block";  // show the loading message.
        $.ajax({
            url: "loadData.php?id=<? echo $id; ?>",
            type: "POST",
            cache: false,
            data: "&page="+ page,
            success : function(html){
                $(".content").html(html);
        document.getElementById("load").style.display = "none";
            }
        });

0

//$(document).ready(function(){
//  $("a").click(function(event){
//  event.preventDefault();
//  $("div").html("This is prevent link...");
// });
//});   

$(document).ready(function(){
 $("a").click(function(event){
  event.preventDefault();
  $.ajax({
   beforeSend: function(){
    $('#text').html("<img src='ajax-loader.gif' /> Loading...");
   },
   success : function(){
    setInterval(function(){ $('#text').load("cd_catalog.txt"); },1000);
   }
  });
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  
<a href="http://www.wantyourhelp.com">[click to redirect][1]</a>
<div id="text"></div>


-5

请确保在 AJAX 调用中进行更改

async: true,
type: "GET",
dataType: "html",

为什么?这怎么回答问题呢? - Liam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接