WebService例子1,JavaScript调用WebService

时间:2021-04-29 08:47:16

  新建WebService,命名为AgeWebService.asmx
在[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]的下面添加
[ToolboxItem(false)]  //ToolboxItem(false) means do not display this control in the IDE's toolbox.
[ScriptService]   //这样为web service创建JavaScript proxy class。proxy class确保你能够从客户端script调用WebService
可以通过http://url_to_site/AgeWebService.asmx/js 来看到 the proxy class

 

然后创建Web Method

 

 

 

Web.config中确保有如下语句


<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"/>
   ...

新建页面

创建Script Manager 对象实例

<asp:ScriptManager ID="ScriptManager1" runat="server">
                <Services>
                    <asp:ServiceReference Path="~/WebService/AgeWebService.asmx" />
                </Services>
            </asp:ScriptManager> 

 

 写javascript.