Home > Article > Web Front-end > The page appears briefly blank when Ajax loads data
This time I will bring you a brief blank page when Ajax loads data. How to solve the problem of brief blank page when Ajax loads data? After using ajax to asynchronously obtain data in the project, sometimes due to data problems or network problems, the page always displays blank. Now use loading pictures to transition this state:
<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 loading state Display
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
How to deal with the 501 error reported by Ajax in cross-domain accessHow to make intelligent keyword matching search in AjaxThe above is the detailed content of The page appears briefly blank when Ajax loads data. For more information, please follow other related articles on the PHP Chinese website!