传递参数的标准方法(jQuery.ajax)

前台

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4.     <title>无标题页</title>
  5.     <script type="text/javascript" src="jquery/jquery-1.2.6.js"></script>
  6.     <script type="text/javascript">
  7.         //这个方法把ajax方法封装一下,方便调用。
  8.         function myajax(){  
  9.             //用此法传递数据
  10.             var arr={
  11.                 "name":"tree",
  12.                 "id":"123"
  13.             };
  14.             $.get(
  15.                 "ajax.aspx",
  16.                 arr,
  17.                 callback,
  18.                 "json"
  19.             );
  20.         }
  21.         //回调函数
  22.         function callback(data){
  23.             $('#response').append(data.name+data.id);
  24.             $('#wait').css("display","none");
  25.         }
  26.         //onload()事件
  27.         $(function(){
  28.             $('#confirm').click(myajax);
  29.         })
  30.         //封装json数据,为了代码清晰,看来这种方法不行。
  31.         /* 似乎是因为数组格式不对。
  32.             var dataStr=jsonData();
  33.             alert(dataStr);
  34.         */
  35.         function jsonData(){
  36.             var jsonStr="";
  37.             jsonStr+="/"name/":";
  38.             jsonStr+="/"tree/"";
  39.             jsonStr+=",";
  40.             jsonStr+="/"id/":";
  41.             jsonStr+="/"123/"";
  42.             jsonStr+="}";
  43.             return jsonStr;
  44.         }
  45.     </script>
  46. </head>
  47. <body>
  48.     <div id="confirm">点击</div>
  49.     <div id="response">接收后台数据</div>
  50.     <div id="wait" class="hide">hello</div>
  51. </body>
  52. </html>

后台

  1. public partial class ajax : System.Web.UI.Page
  2. {
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.         //Response.Write("hello"+Request["name"]);
  6.         Hashtable ht = new Hashtable();
  7.         ht.Add("name", Request.Params["name"]);
  8.         ht.Add("id", Request.Params["id"]);
  9.         Response.Write(CreateJsonParams(ht));
  10.         Response.End();
  11.     }
  12.     private string CreateJsonParams(Hashtable items)
  13.     {
  14.         string returnStr = "";
  15.         foreach (DictionaryEntry item in items)
  16.         {
  17.             returnStr += "/"" + item.Key.ToString() + "/":/"" + item.Value.ToString() + "/",";
  18.         }
  19.         return "{" + returnStr.Substring(0, returnStr.Length - 1) + "}";
  20.     }
  21. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值