45IT.COM- 电脑学习从此开始!
DIY硬件教程攒机经验装机配置
设计Photoshop网页设计特效
系统注册表DOS系统命令其它
存储主板显卡外设键鼠内存
维修显卡CPU内存打印机
WinXPVistaWin7unix/linux
CPU光驱电源/散热显示器其它
修技主板硬盘键鼠显示器光驱
办公ExcelWordPowerPointWPS
编程数据库CSS脚本PHP
网络局域网QQ服务器
软件网络系统图像安全
页面导航: 首页 > 设计学院 > 网络编程 > javascript >

jquery使用ajax解析操作asp.net输出json一例

电脑软硬件应用网 45IT.COM 时间:2010-03-23 09:12 作者:dodo

今天在项目中要使用到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":""}]}
好,下面写一个前台的jquery代码:

<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
出处:www.xueit.com/html/2010-03/33-7442412982010322145205968.html

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
无法在这个位置找到: baidushare.htm
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐知识