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
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 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 displayed in loading status
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Quick solution to the problem that Ajax submits the Form form page and still refreshes
AJAX shows loading And pop up the implementation example of the layer occlusion page
##Method of using ajax to submit the form in the Lavarel framework_AJAX related
The above is the detailed content of Use loading images to solve the problem of temporary blank page during Ajax data loading. For more information, please follow other related articles on the PHP Chinese website!