jquery读取后台代码

前台代码:

  <script type="text/javascript">


        $(function () {
            $("#btn").click(function () {
                $.ajax({
                    //用post方式   
                    type: "Post",
                    //方法所在页面和方法名   
                    url: "Default2.aspx/GetStr",

                   //url:encodeURI("../处理/defalut.aspx/method"),  //路径中有 中文 用 encodeURI编码。
                    contentType: "application/json; charset=utf-8",
                    data: "{a:'aaa',b:'bbb'}",
                    dataType: "json",
                    success: function (data) {
                        //返回的数据   
                        alert(data.d);
                    },
                    error: function (err) {
                        alert(err);
                    }
                });

                //禁用提交   
                return false;
            });
        });

后台代码:

[WebMethod]
    public static string GetStr(string a,string b)
    {
        return a + b;
    }

后台方法为静态 必须加上特性WebMethod.

如果在请求的路劲中包含“中文”,则url要用encodeURI()方法编码。

原文地址:https://www.cnblogs.com/net-study/p/3253044.html