asp.net ajax的使用

参考:https://www.cnblogs.com/acles/articles/2385648.html

https://www.cnblogs.com/xujingyang/p/5560646.html

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