AJAX调用后台,DataTable/DataSet序列化



前台调用


 

url="Handler1.ashx";
$.ajax({
                type: "post",
                url: url,
                data: { "stype": stype, "cityName": escape(cityName), "aqi": aqi },
                dataType: "json", //"application/json", //
                success: function (data, state) {
                    lat = []; lon = [];
                    var d = new Date();
                    var time = d.getDate();
                    var companyList = "";
                    var onLine = 0;
                    var unLine = 0;
                    var total = 0;
                    if (data != "") {
                        var aad = eval(data);
                        $.each(aad, function (i, item) {    //返回sname,Longitude,Latitude,did,isMonitor,stype,city_name,sid,Udate,AQILevel,AQI,aqiTime,sno
                            var id = i + 1;
                            companyList += CreateCompanyList(item, id);
                            lat.push(item.Latitude);
                            lon.push(item.Longitude);
                            onLine++;
                        });
                        total = aad.length;
                        if (pointType == "selected") {//高亮显示
                            ShowHighLight(aad);
                        }
                        else drawPoint(aad); //在地图上标注查询结果
                    }
                    companyList += "</table>";
                    SetCompanyList(companyList);
                    SetCompanyState(total, onLine, unLine);
               },
                complete: function (aa, bb) {
                    arr = aa;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }

            });

几个关键参数解释:

data: 传到后台的参数。

url : 后台地址,即Handler1.ashx文件的地址

dataType: 返回值的类型。如果此参数与后台返回值类型不匹配,会报字符或语法错误


后台方法ASHX文件:


<span style="font-size:18px;"> /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";// "application/vnd.ms-excel";// "text/plain";//
            string stype="",cityName="",aqi="";
            if (context.Request["stype"] != null)
            {
                stype = context.Request["stype"].ToString();
            }
         
            if (context.Request["cityName"] != null)
            {
                cityName =  HttpUtility.UrlDecode(context.Request["cityName"]);
            }
            if (context.Request["aqi"] != null)
            {
                aqi = context.Request["aqi"].ToString();
            }
           // List<Air_nodeInfo> nodeList = GetAir_nodeListAll();
            DataTable names = GetNodeList(stype, cityName, aqi);
           
            JavaScriptSerializer serializer = new JavaScriptSerializer();
           
            string jsonEmp = Serialize(names,false);
            context.Response.Write(jsonEmp);
        }

        public DataTable GetNodeList(string stype, string cityName, string aqi)
        {
            DataTable dt = new DataTable();
            Air_nodeService nodeService = new Air_nodeService();
            dt = nodeService.GetNodeList(stype, cityName, aqi);//sname,Longitude,Latitude,did,isMonitor,stype,city_name,sid,Udate,AQILevel,AQI,aqiTime,sno
            return dt;
        }

        public List<Air_nodeInfo> GetAir_nodeListAll()
        {
            List<Air_nodeInfo> nodeList = new List<Air_nodeInfo>();
            Air_nodeService nodeService = new Air_nodeService();
            nodeList = nodeService.GetAir_nodeListAll();
            return nodeList;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        /// <summary>序列化方法
        /// 不需要分页
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="flag">false</param>
        /// <returns></returns>
        public string Serialize(DataTable dt, bool flag)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    result.Add(dc.ColumnName, dr[dc].ToString());
                }
                list.Add(result);
            }
            return serializer.Serialize(list); ;
        }
        /// <summary>序列化方法
        /// 不需要分页
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="flag">false</param>
        /// <returns></returns>
        public string Serialize(DataSet ds, bool flag)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            ArrayList listss = new ArrayList();
            List<Dictionary<string, object>> list ;
            DataTable[] dt = new DataTable[]{ds.Tables[0],ds.Tables[1]};
            for (int i=0;i< dt.Length;i++)
            {
                list = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dt[i].Rows)
                {
                    Dictionary<string, object> result = new Dictionary<string, object>();
                    foreach (DataColumn dc in dt[i].Columns)
                    {
                        result.Add(dc.ColumnName, dr[dc].ToString());
                    }
                    list.Add(result);
                }
                listss.Add(list);
            }
            return serializer.Serialize(listss); 
        }
    }</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值