通過客戶端JavaScript訪問後臺WebMethod

 前臺代碼如下所示:

<form id="form1" runat="server">   

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

<script language="javascript" type="text/javascript">        

     function GetPageMethod()           

  {             

          PageMethods.ExcuteDBOperation(2, Succeed, Fail);  

                  //succeed 和 fail兩個參數是可以省略的             }             

           //當ExcuteDBOperation執行成功後,會調用該函數,參數result中會記錄ExcuteDBOperation的返回結果             function Succeed(result)             {

                if(result == true)

                {

                        //...

                 }

                //當ExcuteDBOperation執行失敗後,會調用該函數,

                 else

                {                         alert(result);

                  }             }

            function Fail()

           {

                 alert('Failed');

           }         </script>     </form>

//後臺代碼

[System.Web.Services.WebMethod]    

 public static bool  ExcuteDBOperation(int id)   

  {

                  return true;

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章