zoukankan      html  css  js  c++  java
  • ASP.NET前台AJAX方法调用后台的方法写法

    前台:

    <input id="AjaxDemo" type="button" onclick="get()" value="开始" />
            <script type="text/javascript">
                //$().ready(
                //           function () {
                //               $("#AjaxDemo").click(function () {
                //                   $.ajax({
                //                       type: "POST",
                //                       url: "AJAXTest.aspx/ABC",
                //                       data: "{'ABC':'test'}",
                //                       dataType: "json",
                //                       contentType: "application/json; charset=utf-8",
                //                       success: function (msg) { alert(msg.d); }
                //                   })
                //               })
                //           }
                //       )
                $(function () {
                    $("#AjaxDemo").click(function () {
                        $.ajax({
                            type: "POST",
                            url: "AJAXTest.aspx/GetAddress",
                            //data: "{'ABC':'test'}",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            success: function (msg) { alert(msg.d); }
                        })
                    })
                })
            </script>

    后台:

     [WebMethod]
            //[AjaxMethod]
            public static string GetAddress()
            {
                string Address = "hello word";
                //do somthing to get the address here 
                return Address;
            }
    
    
            [WebMethod]
            public static string ABC(string ABC)
            {
                return ABC;
            }

    注意要素:后台方法必须加入webmethod标记,并且为静态方法,而且据我试验了一下貌似GET方式是不可以的,如果有大神可以使用get方式完成这个功能还请指教,而且返回的数据最好是JSON格式的,msg的属性必须是d,具体是为什么暂时不知道,即,msg.d,d是必须的

  • 相关阅读:
    线程&进程
    C#入门基础
    .Net GC垃圾收集机制(下)
    .Net GC垃圾收集机制(上)
    GAC的理解及其作用
    C# DES加密,KEY和IV不同设置的写法
    常见加密算法简析
    密码学
    数字签名是什么?(数字证书)
    加密算法和MD5等散列算法的区别
  • 原文地址:https://www.cnblogs.com/llcdbk/p/4682959.html
Copyright © 2011-2022 走看看