jQuery传值

来源:互联网 发布:python time 时间加减 编辑:程序博客网 时间:2024/05/19 17:09

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;        }    }}



0 0