关于使用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>

posted on 2009-09-13 14:27  ruonanxiao  阅读(453)  评论(0编辑  收藏  举报

导航