Home  >  Article  >  Web Front-end  >  The page appears briefly blank when Ajax loads data

The page appears briefly blank when Ajax loads data

php中世界最好的语言
php中世界最好的语言Original
2018-04-03 09:48:112013browse

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=&#39;list&#39;><a href=&#39;php/phpArticle.php?art="+msg[i][&#39;id&#39;]+" &#39;class=&#39;widget-list-link&#39;>" + msg[i]['title']+"<i class=&#39;more-mark&#39;>"+">>"+"</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 access


How to make intelligent keyword matching search in Ajax

The 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!

Statement:
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