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:'',//提供接口的文件地址链接 dataType:'json', type:'POST', beforeSend: function(){ $('#loading').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){ //数据读取成功并显示数据列表 $('#loading').fadeOut(1000); //图片显示时间; var ul = ""; for(var i= 0;i<msg.legth;i++){ //数据列表行数 ul += "<li class='list'><a href='php/phpArticle.php?art="+msg[i]['id']+" 'class='widget-list-link'>" + msg[i]['title']+"<i class='more-mark'>"+">>"+"</i></a></li>"; } $("#list").html(ul); }, error:function(){ //失败时显示 console.log("链接错误") ; } }); }); </script>
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!