DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
ASP.NET AJAX Beta 2 呼叫本機 Web Service 的寫法變化
編輯:AJAX詳解     

新版 ASP.NET AJAX 在呼叫本機 Web Service 這件事上改動不小,以下依實作步驟說明(沿用官方範例)。

一、建立 Web Service

新增一支 HelloWorldService.asmx:

<%@ WebService Language="C#" Class="Samples.ASPNet.HelloWorldService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace Samples.ASPNet
{
    [WebService(Namespace = "https://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class HelloWorldService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld(String query)
        {
            string inputString = Server.HtmlEncode(query);
            if (!String.IsNullOrEmpty(inputString))
            {
                return String.Format("Hello, you queried for {0}. The " +
                    "current time is {1}", inputString, DateTime.Now);
            }
            else
            {
                return "The query string was null or empty";
            }
        }
    }
}

關鍵在於 [ScriptService] 這個類別層級的屬性。只有加上它,這個服務才會產生可供 JavaScript 直接呼叫的用戶端代理;缺少這一行,前端就拿不到對應的呼叫入口。命名空間則需引用 Microsoft.Web.Script.Services。

二、建立呼叫頁面

接著建立 AjaxScript1.aspx:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
  <head id="Head1" runat="server">
    <title>測試一</title>
    <style type="text/css">
      body { font: 11pt Trebuchet MS; color: #000000;
             padding-top: 72px; text-align: center }
      .text { font: 8pt Trebuchet MS }
    </style>
  </head>
  <body>
    <form id="Form1" runat="server">
      <asp:ScriptManager runat="server" ID="scriptManager">
        <Services>
          <asp:ServiceReference path="~/HelloWorldService.asmx" />
        </Services>
      </asp:ScriptManager>
      <div>
        Search for
        <input id="SearchKey" type="text" />
        <input id="SearchButton" type="button" value="Search"
               onclick="DOSearch()" />
      </div>
    </form>
    <hr style="width: 300px" />
    <div><span id="Results"></span></div>
    <script type="text/javascript">
      function DOSearch() {
        var SrchElem = document.getElementById("SearchKey");
        Samples.ASPNet.HelloWorldService.HelloWorld(SrchElem.value, OnRequestComplete);
      }
      function OnRequestComplete(result) {
        document.getElementById("Results").innerHTML = result;
      }
    </script>
  </body>
</html>

三、最容易踩到的兩個細節

ScriptManager 必須放在 form 內。新版要求 ScriptManager 位於伺服器端表單之中,才能正確註冊服務參考並輸出代理腳本:

<asp:ScriptManager runat="server" ID="scriptManager">
  <Services>
    <asp:ServiceReference path="~/HelloWorldService.asmx" />
  </Services>
</asp:ScriptManager>

呼叫端不再需要自建代理。透過 ServiceReference 宣告服務後,前端會出現與命名空間對應的物件(此例為 Samples.ASPNet.HelloWorldService),方法的最後一個參數就是完成後的 callback 函式。這與早期需要手寫 XML、自己解析回應的做法相比,省下了大量樣板程式碼。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved