Answer to the problem that the page appears briefly blank during Ajax data loading

小云云
Release: 2023-03-19 12:14:01
Original
2105 people have browsed it

After using ajax to asynchronously obtain data in the project, sometimes the page always displays blank due to data problems or network problems. Now use loading images to transition this state. Let’s learn the specific example code through this article. I hope it can help everyone. .


<script>
  $(function(){
    $.ajax({
      url:&#39;&#39;,//提供接口的文件地址链接
      dataType:&#39;json&#39;,
      type:&#39;POST&#39;,  
      beforeSend: function(){
        $(&#39;#loading&#39;).html("<img src=\"images/loading.gif\" width=\"15%\" style=\"text-align:center;margin:0 auto;\"><p style=\"color:#999;font-size:14px\">加载中,请稍后……</p>") //数据发送过程中先加载图片 
      },
      error: function(msg){ //数据读取失败显示
        zNodes=data.responseJson;
        alert("对不起,数据获取失败,请联系管理员");
      },
      success:function(msg){ //数据读取成功并显示数据列表
        $(&#39;#loading&#39;).fadeOut(1000);  //图片显示时间;
        var ul = "";
        for(var i= 0;i<msg.legth;i++){ //数据列表行数
          ul += "<li class=&#39;list&#39;><a href=&#39;php/phpArticle.php?art="+msg[i][&#39;id&#39;]+" &#39;class=&#39;widget-list-link&#39;>" + msg[i][&#39;title&#39;]+"<i class=&#39;more-mark&#39;>"+">>"+"</i></a></li>";
        } 
          $("#list").html(ul);
      },
      error:function(){ //失败时显示
        console.log("链接错误") ;
      }
    });
  });
</script>
Copy after login

Data is displayed in loading status

##Related recommendations:


How to play with ajax data request of WeChat applet

How to use json to interact with front-end Ajax data in Java

php uses ajax data Summary of common methods for submitting posts and posts

The above is the detailed content of Answer to the problem that the page appears briefly blank during Ajax data loading. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template