首页新闻招聘找找看知识库

自己做的一个ajax+mysql分页显示,运行结果弹窗显示oject HTMLdivElement ,求大神帮忙

0
[已解决问题] 解决于 2017-12-16 16:02

这是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();

    }
}
47谢远鹏的主页 47谢远鹏 | 菜鸟二级 | 园豆:202
提问于:2017-12-08 14:40
< >
分享
最佳答案
1

error:function(http,mes,exp){

  alert(msg+" "+exp);//msg应该改为mes

}

奖励园豆:5
我只是慕名而来 | 菜鸟二级 |园豆:295 | 2017-12-08 19:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册