asp.net script方法調用webService方法

來源:互聯網
上載者:User

    已經做過幾次了,但是每次都要忘記一些,今天把這些記錄一下好了。

    (1),添加一個web服務,XXX.asmx.

      (2), 添加system.web.extensions引用

      (3), 修改XXX類的屬性為[ScriptService]

      (4),  隨意寫一個方法webMethod(),將方法屬性修改為[WebMethod]

      (5),    添加test.aspx頁面,向form中添加webservice引用

               :  <asp:ScriptManager ID="ScriptManager1" runat="server">
                          <Services>
                                <asp:ServiceReference Path ="XXX.asmx.asmx"  InlineScript ="true" />
                          </Services>
                    </asp:ScriptManager>

       (6),寫js方法,TestMethod();裡面可以調用我們的webMethod方法;但是調用方法的時候要使用非同步呼叫的回呼函數名作為參數。

                 :function TestMethod(TestCallBack)

                     {

                        XXX.webMethod();

                       }

                   :fuction TestCallBack(result)

                    {

                         //method

                      }

     (7),webconfig 中System.Web節點中添加以下代碼

           <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
      </httpHandlers>

聯繫我們

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