zoukankan      html  css  js  c++  java
  • 关于使用Ajax 用js调用web服务

      一下为主要要素:
         1.必须添加程序集 System.Web.Extensions.Design.dll System.Web.Extensions.dll 引用
         2.配置文件必须添加
      <system.web> 
        <httpHandlers>
          <!--ajax的配置開始 -->
          <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"/>
          <!--ajax的配置結束 -->
        </httpHandlers>
     <compilation debug="false">
          <assemblies>
            <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
          </assemblies>
        </compilation>
      </system.web> 
      3. webservices类必须添加下列属性 命名空间为using System.Web.Script.Services;
    [ScriptMethod()]
    public class DataSorceFactory : System.Web.Services.WebService
    {
    }
      4.aspx页面的调用
        <script type="text/javascript">
            function Show() {
                DataSorceFactory .Test(onsuccess, onFailed, null);
            }
            ///成功調用的方法
            function onsuccess(result) {
                alert(result);
            }
            ///失败调用的方法
            function onFailed(result) {
                alert("错误信息"+result.get_message());
            }
        </script>

    cs方法
    [ScriptMethod()]
    public class DataSorceFactory : System.Web.Services.WebService
    {
        [WebMethod]
        public string Test()
        {
            return "歡迎你 啊戴爾!";
        }
    }
      5.aspx页面的form必须添加
     <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/DataSorceFactory .asmx" />
            </Services>
        </asp:ScriptManager>
    </form>
  • 相关阅读:
    vue 手动挂载 $amount()
    Redis 主从配置
    DMA分区管理
    C# 构造函数里的base和this的区别
    SQL Server 数据库性能优化
    TCP和UDP的优缺点及区别
    Django框架初步应用简述
    前端vue框架应用雏形
    接口mock之moco
    python进阶(九)~~~协程、进程池、线程/进程/协程对比
  • 原文地址:https://www.cnblogs.com/ruonanxiao/p/1565800.html
Copyright © 2011-2022 走看看