客户端调用webservice

时间:2021-05-07 16:11:34
    DEMO :客户端调用 webservice

服务器端:

l         导入命名空间:using System.Web.Script.Services;

l         加入属性:[ScriptService]、[ScriptMethod]

客户端:

l          使用代理:

<asp:ScriptManager ID="ScriptManager1" runat="server" >

              <Services>

                   <asp:ServiceReference Path=" EmployeeService.asmx" />

              </Services>

</asp:ScriptManager>

l          调用函数:

<script language="javascript" type="text/javascript">

              function showEmployee(firstName, lastName, title)

              {    //命名空间.类名.方法名

                   AspNetAjaxOverview.EmployeeService.GetEmployee(

                       firstName,

                       lastName,

                       title,

                       onGetEmployeeSuccess);

              }            

              function onGetEmployeeSuccess(employee)

              {

                   alert(String.format(

                       "Hello I'm {0} {1}, my title is '{2}'",

                       employee.FirstName,

                       employee.LastName,

                       employee.Title));

              }

     </script>

l          触发事件: <input type="button" value="Bill Gates" onclick="showEmployee('Bill',  'Gates','Chair man')" />