預設成功回呼函數和預設失敗回呼函數以及逾時的使用

來源:互聯網
上載者:User

範例非常簡單,所以直接看代碼,注釋也非常詳細
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]   
[System.Web.Script.Services.ScriptService]
public class Demo__7__Use : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod]
    public string GetCurrentTime()
    {
        return DateTime.UtcNow.ToString();
    }

    [WebMethod]
    [ScriptMethod]
    public string SetTimedOut()
    {
        //設定沉睡時間為 3 s
        System.Threading.Thread.Sleep(3000);
        return "BaoBeiMe";
    }


HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

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

        function PageLoad() {
            //設定預設的回調失敗後的處理
            深入淺出_ASP.NET_AJAX.Demo__7__Use.
                         set_defaultFailedCallback(OnFailed);
           //設定預設的回調成功後的處理
            深入淺出_ASP.NET_AJAX.Demo__7__Use.
                         set_defaultSucceededCallback(OnSucceded);
           //設定 2 s 後無響應則逾時
            深入淺出_ASP.NET_AJAX.Demo__7__Use.
                         set_timeout(2000);
        }
        function Button1_onclick() {

            //通過調用 WebService 來擷取伺服器當前的時間
            深入淺出_ASP.NET_AJAX.Demo__7__Use.GetCurrentTime();
        }

        //預設的成功的回呼函數
        function OnSucceded(result) {
            alert(result);
        }

       //預設的失敗的回呼函數
        function OnFailed(error) {
            var msg = String.format("錯誤資訊    {0}\n異常類型    {1}" +
                                    "\n逾時    {2}\n狀態代碼    {3}",
                             error.get_message(),
                             error.get_exceptionType(),
                             error.get_timedOut(),
                             error.get_statusCode());
            alert(msg);
        }
        function Button2_onclick() {
            //通過調用 WebService 來在伺服器端設定逾時後查看逾時的效果
            深入淺出_ASP.NET_AJAX.Demo__7__Use.SetTimedOut();
        }

    </script>

</head>
<body onload="PageLoad()">
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="Demo__7__Use.asmx" />
        </Services>
    </asp:ScriptManager>
    <div>
        <input id="Button1" type="button" value="擷取伺服器目前時間"
               onclick="return Button1_onclick()" /><br />
        <br />
        <input id="Button2" type="button" value="設定逾時後的效果"
               onclick="return Button2_onclick()" /><br />
    </div>
    </form>
</body>
</html>

 

 

                                                 2010—1—26

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.