简体   繁体   English

当 AJAX 呼叫正在进行时显示'加载中'图像

[英]Display 'loading' image when AJAX call is in progress

I have the following jQuery Ajax call:我有以下 jQuery Ajax 电话:

$.ajax({
                   type: "POST",
                   url: "addtobasket.php",
                   data: "sid=<?= $sid ?>&itemid=" + itemid + "&boxsize=" + boxsize + "&ext=" + extraval,
                   success: function(msg){
                     $.post("preorderbasket.php", { sid: "<?= $sid ?>", type: "pre" },
                     function(data){
                        $('.preorder').empty();
                         $('.preorder').append(data); 
                     }); 
                   }
                 });

I want to display an image when the ajax call is in progress.我想在 ajax 通话进行时显示图像。 How can I do that?我怎样才能做到这一点?

Thanks,谢谢,

try this:尝试这个:

<script type="text/javascript">
    $(document).ready(function()
    {
        $('#loading')
            .hide()
            .ajaxStart(function() {
                $(this).show();
            })
            .ajaxStop(function() {
                $(this).hide();
            });
    });
</script>

<div id="loading">
        Loading....
</div>

This will show the loading image each time you are doing an ajax request, I have implented this div at the top of my pages, so it does not obstruct with the page, but you can always see when an ajax call is going on.这将在您每次执行 ajax 请求时显示加载图像,我在页面顶部实现了此 div,因此它不会阻碍页面,但您始终可以看到 ajax 调用何时进行。

Something along these lines ( showLoadingImage and hideLoadingImage are up to you):这些方面的东西( showLoadingImagehideLoadingImage取决于你):

// show the loading image before calling ajax.
showLoadingImage();

$.ajax({
    // url, type, etc. go here
    success: function() {
        // handle success. this only fires if the call was successful.
    },
    error: function() {
        // handle error. this only fires if the call was unsuccessful.
    },
    complete: function() {
        // no matter the result, complete will fire, so it's a good place
        // to do the non-conditional stuff, like hiding a loading image.

        hideLoadingImage();
    }
});

You can display the image just before this call to $.ajax() and then hide/remove the image in the post handler function (just before your.empty()/.append(data) calls.您可以在调用 $.ajax() 之前显示图像,然后在后处理程序 function 中隐藏/删除图像(就在 your.empty()/.append(data) 调用之前。

It works.有用。 I have tested it.我已经测试过了。

<script type='text/javascript'>

$(document).ready(function(){

$("#but_search").click(function(){
var search = $('#search').val();

$.ajax({
 url: 'fetch_deta.php',
type: 'post',
data: {search:search},
beforeSend: function(){
 // Show image container
$("#loader").show();
},
success: function(response){
 $('.response').empty();
$('.response').append(response);
},
complete:function(data){
// Hide image container
$("#loader").hide();
}
});

});
});
</script>

<input type='text' id='search'>
<input type='button' id='but_search' value='Search'><br/>

<!-- Image loader -->
<div id='loader' style='display: none;'>
  <img src='reload.gif' width='32px' height='32px'>
</div>
<!-- Image loader -->

<div class='response'></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM