Home >Web Front-end >JS Tutorial >Use loading images to solve the problem of temporary blank page during Ajax data loading

Use loading images to solve the problem of temporary blank page during Ajax data loading

亚连
亚连Original
2018-05-23 10:33:402174browse

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:&#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>

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!

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