今天在项目中要使用到jquery来操作json,首先通过jquery的ajax来请求asp.net输出数据json格式,之后jquery来操作json数据。 首先看下asp.net的文件CustomerEdit_sale.aspx后台代码 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string action = Utils.Request("action"); if (action == "getcustomerbasic") //ajax请求客户基本信息 { this.customerBasic(Utils.Request("wd")); Utils.End(); } } } //输出json private void customerBasic(string strCustID) { Response.ContentType = "text/plain"; string strJson = string.Empty; TelSales telSale = new TelSales(); DataSet dsCust = telSale.QueryCustomers("customer_id=" strCustID); strJson = Utils.DataTableToJson("info", dsCust.Tables[0]); Response.Write(strJson); } 以上代码asp.net把datatable转为json的代码在这篇文章的DataTableToJson方法: 前台调用输出的内容为: {"info":[{"customer_id":"1883294","customer_name":"","last_incoming_date":"2009-10-19 15:49:00","customer_sex":"1","salesman":"6a66f4d0-720f-4e10-8161-fadd7a3464cc","customer_type":"D","province":"湖南省","city":"长沙市","stock_money":"5","media_id":"189","birthday":"1900-1-1 0:00:00","saleman":"","is_filing":"True","is_new":"True","customer_business":"未知","customer_age":"未知","remark":"","customer_IdCard":"","stock_age":"未知","mail_box":"","post_code":"","product_id":"","address":"","stock_software":"","source_level":"","max_source_level":"","media_name":"","advertising_name":""}]} <script> //ajax加载客户基本信息 function customerinfoAjax(cid) { $.post("CustomerEdit_sale.aspx", { "action": "getcustomerbasic", "wd": cid }, function(data) { $.each(data.info, function(i) { //读取某一下json的值 alert(data.info[i].last_incoming_date); }); }, "json"); //指定ajax返回数据为json对象 } customerinfoAjax("1883294"); </script> 效果图: 作者:dodo |