C# ajax的使用

1.请求aspx页面后台中的方法,带参数

前台

$.ajax({
                type: "Post",
                url: "ResponsePage.aspx/RequestMethod1",
                data:"{'msg':'hello'}",
                contentType: "application/json;charset=utf-8",// 这句可不要忘了。
                dataType: "json",
                success: function (res) {
                    $("#dataShow").text("success:" + res.d); // 注意有个d,至于为什么通过chrome看响应吧,O(∩_∩)O。
                },
                error: function (xmlReq, err, c) {
                    $("#dataShow").text("error:" + err);
                }
            });

后台:

[WebMethod]
public static string RequestMethod1(string msg)
{
      return msg;
 }

2.一般处理程序(ashx)

前台

function test(id,pwd){
            $.ajax({
                url: '/Tools/Handler.ashx?action=test',
                data:{id : id, pwd : pwd },
                success: function (data){
                    alert(data.data);
                }
            });
        }

后台

HttpRequest _request;
    HttpResponse _response;
    public void ProcessRequest(HttpContext context)
        {
           _request = context.Request;
           _response = context.Response;
            context.Response.ContentType = "application/json";
            string action = context.Request["action"].ToString();
//string action = _request.QueryString["action"] + "";
switch (action) { case "test": test(context); break; default: break; } } public void test(HttpContext context) { string id = context.Request["id"].ToString(); string pwd = context.Request["pwd"].ToString(); ///代码段 /// List<int> list = new List<int>(); list.add(1); vat data = JsonConvert.SerializeObject(list); HttpContext.Current.Response.Write("data:"+data); }

 

转载于:https://www.cnblogs.com/zhang1f/p/11115413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值