Asp.net AJAX調用後台靜態方法總結

來源:互聯網
上載者:User
文章目錄
  • 1.Ajax Library方式
  • 2. jQuery方式
  • 3. 還有一種好像是要引用AJAX.dll檔案的. 在後台註冊前台方法. 這個好像在.net2.0的時候用的比較多. 具體沒仔細研究.
從用戶端調用後台靜態方法
1.Ajax Library方式

C#代碼:

[WebMethod]public static DateTime GetCurrentTime(string str){    return DateTime.Now;}

JS代碼:

    <form id="form1" runat="server">    <script language=javascript type="text/javascript">        function GetCurrentTime1() {            PageMethods.GetCurrentTime('NewEgg ajax training', CheckIsSuccess);        }        function CheckIsSuccess(result) {            alert(result);        }     </script>    <div>        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">        </asp:ScriptManager>        <input id="Button1" type="button" value="用戶端控制項調用伺服器端的方法" onclick="GetCurrentTime1()" />    </div>    </form>

說明:

C#方法必須加 "[WebMethod]"

前台頁面必須使用引用 伺服器控制項

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

</asp:ScriptManager>

調用方法: PageMethods.後台方法名(參數[,參數....], 成功後調用的方法名);

Ajax Library

2. jQuery方式

C#代碼:

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

JS代碼:

$().ready(            function() {                $("#AjaxDemo").click(function() {                    $.ajax({                        type: "POST",                        url: "Default.aspx/ABC",                        data: "{'ABC':'test'}",                        contentType: "application/json; charset=utf-8",                        success: function(msg) {alert(msg); }                    })                })            }        )

說明: 必須引用jQuery庫檔案.

3. 還有一種好像是要引用AJAX.dll檔案的. 在後台註冊前台方法. 這個好像在.net2.0的時候用的比較多. 具體沒仔細研究.

還望有其他更簡單方法的同學互相交流下~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.