Atlas 學習之006:Make Client-Side Network Callbacks with “Atlas”

來源:互聯網
上載者:User

  下面的文章將介紹:

  how Microsoft “Atlas” helps web developers make network callbacks directly from client-side script code.

  本文將學習以下內容:

  • how to create a simple web service that is “Atlas” enabled;
  • how to call this web service from client-side JavaScript code; 
  • how to use the data returned from the web service.

 

  詳細步驟:

1、先見一個簡單的web service,只將HelloWorld方法小改如下:

 [WebMethod]
    public string HelloWorld(String ss)
    {
        return "Hello " + ss;
    }

 

2、 在“源”頁面裡,把代碼“<atlas:ScriptManager ID="ScriptManager1" runat="server" / >”修改成如下代碼:

<atlas:ScriptManager ID="ScriptManager1" runat="server" >
            <Services>
                <atlas:ServiceReference Path="WebService.asmx" />
            </Services>
        </atlas:ScriptManager>

 

3、在“設計”頁面裡從“ HTML  ” 選項卡裡拖拽一個“text”和“button”控制項,雙擊“button”控制項,切換到“源”頁面,把代碼“function Button1_onclick() {    }”修改成如下代碼:

function Button1_onclick()
{
    abc = WebService.HelloWorld(document.getElementById('Text1').value,OnComplete,OnTimeOut);
    return false;
}

function OnComplete(result)
{
    alert(result);
}

function OnTimeOut(result)
{
    alert("逾時!");
}

 

4、儲存所有檔案, F5運行一把,在“text”裡輸入任一字元,點擊按鈕,檢驗一下效果。

完整代碼:

 

<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">...
// <!CDATA[

function Button1_onclick() 
...{
    abc = WebService.HelloWorld(document.getElementById('Text1').value,OnComplete,OnTimeOut);
    return false;
}

function OnComplete(result)
...{
    alert(result);
}

function OnTimeOut(result)
...{
    alert("逾時!");
}

// ]]>
</script>
</head>
<body>
    <form id="form1" runat="server">
        <atlas:ScriptManager ID="ScriptManager1" runat="server" >
        <Services>
        <atlas:ServiceReference  Path="WebService.asmx" />
        
        </Services>
        </atlas:ScriptManager>
        <br />
        請輸入你的姓名:
        <input id="Text1" type="text" />
        <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /></form>    
</body>
</html>

聯繫我們

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