这是HTML代码
<!DOCTYPE html> <html> <head> <script language="JavaScript" src="jquery-1.12.0.min.js"></script> <script language="JavaScript"> var pageIndex=0; var pageSize=10; var pageCount=0; function display(){ try{ $.ajax({ type:'get', url:'server.ashx?pageIndex='+pageIndex.toString()+'&rnd='+Math.random().toString(), data:{}, async:true, success:function(resp){ try{ var obj = evel("("+resp+")"); if(obj.state=="OK"){ s="<table border='0' align='center' style='font-size:12px;'>"; s=s+"<tr><td width='50'>序号</td><td width='100'>省份</td><td width='100'>城市</td></tr>" pageCount=parseInt(obj.count); for(var i=0;i<obj.zones.length;i++){ var p=obj.zones[i].province; var c=obj.zones[i].city; s=s+"<tr><td>"+(pageIndex*pageSize+i+1).toString()+"</td><td>"+p+"</td><td>"+c+"</td></tr>"; } s=s+"</table>"; //显示表格 $("#msg").html(s); //显示当前页码 $("#page").html("第"+(pageIndex+1).toString()+"/"+pageCount.toString()+"页"); } else alert(obj.state); }catch(exp) { alert("123"); } }, error:function(http,mes,exp){ alert(msg+" "+exp); } }); }catch(exp) {alert("456");} } function goFirst(){ if(pageIndex>0){ pageIndex=0; display(); } } function goPrev(){ if(pageIndex>0){ pageIndex--; display(); } } function goNext(){ if(pageIndex<pageCount-1){ pageIndex++; display(); } } function goLast(){ if(pageIndex<pageCount-1){ pageIndex=pageCount-1; display(); } } $(document).ready(display); </script> </head> <body> <div align="center"> <input type="button" value="第一页" onclick="goFirst()"/> <input type="button" value="上一页" onclick="goPrev()"/> <input type="button" value="下一页" onclick="goNext()"/> <input type="button" value="最末页" onclick="goLast()"/> <span id="page"></span> </div> <div id="msg"></div> </body> </html>
这个是我的服务器server.ashx代码
<%@WebHandler Language="C#" Class="Handler"%> using System; using System.Web; using System.Text; using MySql.Data.MySqlClient; public class Handler : IHttpHandler{ public bool IsReusable{ get{return true;} } public void ProcessRequest(HttpContext context){ string s=""; try{ s = context.Request.QueryString["pageIndex"]; if(s==""||s==null) s="0"; int pageIndex=int.Parse(s); int pageSize=10; int i=0,j=pageIndex * pageSize; string connection = "server=localhost;Database =zone;uid=admin;pwd=123456;charset=utf8"; MySqlConnection conn = new MySqlConnection(connection); conn.Open(); string sqlQuery = "SELECT id,provinced,province FROM provinces"; MySqlCommand cmd = new MySqlCommand(sqlQuery, conn); MySqlDataReader reader = cmd.ExecuteReader(); s = ""; while (reader.Read()){ if(i>=j && i<j+pageSize){ if(s!="") s=s+","; s=s+"{\"id\":\""+reader["id"].ToString()+",\"provinced\":\""+reader["provinced"].ToString()+",\"province\":\""+reader["province"].ToString()+"\"}"; } ++i; } int pageCount=i/pageSize; if(i%pageSize!=0) ++pageCount; s="{\"state\":\"OK\",\"Count\":"+pageCount.ToString()+",\"zone\":["+s+"]}"; reader.Close(); conn.Close(); }catch(Exception exp){ s="{\"state\":error:"+exp.Message+"}"; } context.Response.Clear(); context.Response.ContentType="text/plain"; context.Response.Write(s); context.Response.Flush(); } }
error:function(http,mes,exp){
alert(msg+" "+exp);//msg应该改为mes
}