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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM