Home > Web Front-end > JS Tutorial > AJAX implements data paging without refreshing

AJAX implements data paging without refreshing

php中世界最好的语言
Release: 2018-04-04 16:05:44
Original
1835 people have browsed it

This time I will bring you AJAX to implement non-refresh data paging, and what are the precautions for AJAX to implement non-refresh data paging. The following is a practical case, let's take a look.

I have used the GridView control before when using Asp.Net. This control has its own paging function. Although it is ugly, it is still very powerful. Here, I will show you a more powerful way - using AJAX to directly get data from the server for paging without refreshing.

1. Implementation process

Note: The following contents are all used within the server.

First, create several files in the server path, for example, page1.txt, page2.txt, page3.txt.

Put an array into each file, as follows:

[{user:'blue',pass:'123'},{user:'aaa',pass:'dsfaa'},{user:'Ares',pass:'12346'}]
Copy after login

Figure 1 Contents of page one

Front-end HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX实现分页、</title>
<script src="ajax.js"></script>
<script>
window.onload=function ()
{
  var oUl=document.getElementById('ul1');
  var aBtn=document.getElementsByTagName('a');
  var i=0;
  for(i=0;i<aBtn.length;i++)
  {
    //给每一个按钮附一个初值索引
    aBtn[i].index=i;
    aBtn[i].onclick=function ()
    {
      //调用AJAX函数
      ajax(&#39;page&#39;+(this.index+1)+&#39;.txt&#39;, function (str){
        //获得其中的数据
        var aData=eval(str);
        oUl.innerHTML=&#39;&#39;;
        for(i=0;i<aData.length;i++)
        {
          var oLi=document.createElement(&#39;li&#39;);
          oLi.innerHTML=&#39;<strong>'+aData[i].user+'</strong><i>'+aData[i].pass+'</i>';
          oUl.appendChild(oLi);
        }
      });
    };
  }
};
</script>
</head>
<body>
<ul id="ul1">
</ul>
<a href="javascript:;">1</a>
<a href="javascript:;">2</a>
<a href="javascript:;">3</a>
</body>
</html>
Copy after login
Encapsulated AJAX, JavaScript code:

/*
AJAX封装函数
url:系统要读取文件的地址
fnSucc:一个函数,文件取过来,加载完会调用
*/
function ajax(url, fnSucc, fnFaild)
{
  //1.创建Ajax对象
  var oAjax=null;
  if(window.XMLHttpRequest)
  {
    oAjax=new XMLHttpRequest();
  }
  else
  {
    oAjax=new ActiveXObject("Microsoft.XMLHTTP");
  }
  //2.连接服务器
  oAjax.open('GET', url, true);
  //3.发送请求
  oAjax.send();
  //4.接收服务器的返回
  oAjax.onreadystatechange=function ()
  {
    if(oAjax.readyState==4) //完成
    {
      if(oAjax.status==200)  //成功
      {
        fnSucc(oAjax.responseText);
      }
      else
      {
        if(fnFaild)
          fnFaild(oAjax.status);
      }
    }
  };
}
Copy after login
Final effect:

Figure 2 Display effect

2. Summary

AJAX still requires more attempts and more practice. Pagination is a good idea, it can improve the speed of access, it is a good method, and it will bring better benefits to everyone in the future.

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 avoid being intercepted by the browser when the ajax callback opens a new form

Discussion on Ajax and research

The above is the detailed content of AJAX implements data paging without refreshing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template