jQuery传值

1、前台传值给后台,后台返回字符串

js:

<pre name="code" class="javascript"><script type="text/javascript">                  
                
    $.get("Default.aspx", { typename: "type1", describe: "传值:", res: "成功" },
        function (data) {  //此处是回调函数 接收从后台传回的值  
            alert(data);  //接回来的值是一串字符串                                        
        });
    //$.ajax({
    //    url: 'GetChart.ashx'
    //    data: { "typename": "type1", "describe": "传值:", "res": "成功"},
    //    success: function (data) {
    //        alert(data);
    //    }
    //});
</script>


 



 

cs:

protected void Page_Load(object sender, EventArgs e)
    {
        ajax();
    }
    private void ajax()
    {
        string type= Request["typename"];  //最得前台的JS的第一个参数  

        if (!string.IsNullOrEmpty(type) && type== "type1")  //判断是否通过前台的点击事件进来的  

        {
            Response.Write( Request["describe"].ToString()  + Request["res"].ToString());

            Response.End();

        }

    }

2、前台传值给后台,后台返回数组

js:

<script type="text/javascript">  
    $.ajax({ url: "GetChart.ashx",
        cache: true,
        dataType: "text",
        type: "post",
        data: { "type": 'dme' ,"test":'ddd'},
        success: function (data) {
            //arr = data.split("+");
            //var arrlen = arr.length;//获取字符串
            //转化json数组为普通数组
            var jsonobj = eval('(' + data + ')');
            var labels = [];
            for (var i = jsonobj.length - 1; i >= 0; i--) {
                var record = jsonobj[i].record;
                var time = jsonobj[i].time;
                var type = jsonobj[i].type.toString().replace(/\s+/g, "");
            }
        }
    })


</script>

cs:

<%@ WebHandler Language="C#" Class="GetChart" %>


using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Newtonsoft.Json;


public class GetChart : IHttpHandler
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string test = context.Request.Form["type"];
        string ttt = context.Request.Form["test"];
        
        //if (context.Request.Form["type"] != null)
        //{
            DataTable dt = SqlHelper.ExecuteTableProc("[dbo].[usp_eoffice_ChartRead]");
            int m = dt.Rows.Count;
            string key = "";
            //读取数据库转为字符串
            //if (dt.Rows.Count > 0)
            //{
            //    string[][] bbb=new string[m][];
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        for (int j = 0; j < 2; j++)
            //        {
            //            key += (dt.Rows[i][j].ToString().Trim()+"+");
                        
            //        }
            //    }
            //    string test = key;
            //    context.Response.Write(key);
            //}
            
            //读取数据库转为json数组
            List<Chart> lst = new List<Chart>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lst.Add(new Chart(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString()));
            }
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(lst);
            context.Response.Write(json);
        //}
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }


}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值