电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > javascript > 正文
[图文]jquery使用ajax解析操作asp.net输出json一例
jquery使用ajax解析操作asp.net输出json一例
2010-3-23 9:12:46  文/dodo   出处:学IT网   

今天在项目中要使用到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

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    js或jquery获取网页控件的offset值的…
    验证JS中Object和Function的关系的代…
    使用jquery的Dialog出现内存泄露问题…
    如何优化jQuery性能的12种方法解析
    不支持ASP的空间能用JS吗?
    关于20个Ajax关键问题的疑问及解答
    extjs重新设置按钮button的text的方…
    58项经典的JS代码
    详解总结使用jquery的ajax调用webse…
    为jQuery-easyui的tab组件添加右键菜…
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号