javascript教程AJAX常用技术 AJAX框架技术
返回首页
当前位置: 首页 > AJAX教程 > AJAX常用技术 >

Ajax处理后台返回的Json数据

时间:2013-05-24 11:32来源:未知 作者:admin 点击:我要投稿  高质量的ASP.NET空间,完美支持1.0/2.0/3.5/4.0/MVC等

 /// <summary>        

/// 将对象序列化成Json格式字符串        

/// </summary>        

/// <typeparam name="T"></typeparam>        

/// <param name="t"></param>        

/// <returns></returns>        

public static string JsonSerializer<T>(T t)        

{            

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));            

MemoryStream ms = new MemoryStream();            

ser.WriteObject(ms, t);            

string jsonStr = System.Text.Encoding.UTF8.GetString(ms.ToArray());            

ms.Close();            

string p = @"\\Data\((\d+)\+\d|\)\\/";            

MatchEvaluator mc = new MatchEvaluator(ConvertJsonDateToDateString);            

Regex reg = new Regex(p);            

return reg.Replace(jsonStr, mc);        

}

       

/// <summary>        

/// 日期格式转换        

/// </summary>        

/// <param name="m"></param>        

/// <returns></returns>        

public static string ConvertJsonDateToDateString(Match m)        

{            

string ss = string.Empty;            

DateTime dt = DateTime.Parse(m.Groups[0].Value);            

dt = dt.ToUniversalTime();            

TimeSpan ts = dt - DateTime.Parse("1970-01-01");            

return string.Format("\\/Date({0}+0800)\\/",ts.TotalMilliseconds);        

}

下面以一个例子为例:

[WebMethod]
public string HelloWorld(string ss)
{
            string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ToString();
            SqlConnection conn = new SqlConnection(conStr);
            SqlDataAdapter da = new SqlDataAdapter("select * from member", conn);//查询Member表
            DataTable dt = new DataTable();
            da.Fill(dt);
            //必须设置TableName,此设置为Member表名,可随意
            dt.TableName = "Member";
            return JsonSerializer<DataTable>(dt);
}

那么在前台网页获取数据时

$(function () {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json",
                            dataType: "json",
                            data: "{}",
                            url: "WebService1.asmx/HelloWorld",
                            success: function (data) {
                                var data = $.parseJSON(data.d);//必须要
                                $("div").html("");
                                var re = "<table>";
                                //find("Member")与之前设置的TableName名称一致,MemberId、MemberName为表中字段名
                                $.each($(data).find("Member"), function (temp, i) {
                                    var ss = $(i).find("MemberId")[0].innerText;
                                    re = re + "<tr><td>" + $(i).find("MemberId")[0].innerText + "</td><td>" + $(i).find("MemberName")[0].innerText + "</td></tr>";
                                });
                                re = re + "</table>";
                                $("div").html(re);
                            },
                            error: function (data) { alert(data.responseText); }
                        });
        });

另注:当data参数中包含有单引号时,首先在data中用escape进行编码,然后在读取时用Server.UrlDecode进行解码即可。

本站推荐文章:
本站热点文章:
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色 情、暴力、反动的言论。
评价:
表情:
用户名:密码: 验证码:点击我更换图片