jQuery 在加载数据之前显示 Ajax 加载器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11515994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:42:01  来源:igfitidea点击:

Display Ajax loader before load data

ajaxjquery

提问by Kamal

Hello friends i want to show Ajax loader before Data load in particular div but problem is the data is coming dynamically on same page but my script calling data from another file Script.phpplease see my code below

你好朋友,我想在数据加载之前显示 Ajax 加载器,特别是 div,但问题是数据在同一页面上动态出现,但我的脚本从另一个文件调用数据, Script.php请参阅下面的代码

Script

脚本

<script>
function loadingAjax(div_id)
{
    $("#"+div_id).html('<img src="ajax-loader.gif"> saving...');
    $.ajax({
        type: "POST",
        url: "script.php",
        data: "name=John&id=28",
        success: function(msg){
            $("#"+div_id).html(msg);
        }
    });
}
</script> 

HTML

HTML

<body onload="loadingAjax('myDiv');">


<div id="myDiv"></div>

<div id="xyz"><img src="ss/image/abc.jpg" /></div>


</body>

Its working fine but i want to load data in same page please help me

它工作正常,但我想在同一页面加载数据请帮助我

Thanks in advance ....

提前致谢 ....

EDIT

编辑

I want to show loader before load data #xyzinto #myDiv

我想说明之前加载数据装载#xyz#myDiv

回答by saihgala

you can try with following html -

您可以尝试使用以下 html -

<body onload="loadingAjax('myDiv');">
    <div id="myDiv">
        <img id="loading-image" src="ajax-loader.gif" style="display:none;"/>
    </div>
</body>

and the script -

和脚本 -

<script>
function loadingAjax(div_id) {
      var divIdHtml = $("#"+div_id).html();
      $.ajax({
           type: "POST",
           url: "script.php",
           data: "name=John&id=28",
           beforeSend: function() {
              $("#loading-image").show();
           },
           success: function(msg) {
              $("#"+div_id).html(divIdHtml + msg);
              $("#loading-image").hide();
           }
      });
}
</script> 

回答by Nguy?n Tr??ng

you can try with following script

您可以尝试使用以下脚本

<script>
function ajax()
{
    var options = {};
    options.url = '';
    options.type = 'post';
    options.data = '';
    options.dataType = 'JSON'; // type data get from sever
    options.contentType = 'application/json';// type data post to sever
    options.async = false;
    options.beforeSend = function () {

    };

    options.success = function (data)
    {

    };
    options.error = function (xhr, status, err) {

        console.log(xhr.responseText);
    };
    $.ajax(options);
}
</script>