asp.net 练习 js 调用webservice
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- /// <summary>
- ///JsWebServiceTest 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
- [System.Web.Script.Services.ScriptService]
- public class JsWebServiceTest : System.Web.Services.WebService {
- public JsWebServiceTest () {
- //如果使用设计的组件,请取消注释以下行
- //InitializeComponent();
- }
- [WebMethod]
- public string HelloWorld() {
- return "Hello World";
- }
- [WebMethod]
- public string WelCome(string name)
- {
- return name+":欢迎光临";
- }
- }
注意事项 js调用webservice 方法 要取消注释 system.web.script.services.scriptservice
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebServiceForm2.aspx.cs" Inherits="test2_WebServiceForm2" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- <!--引用web 服务 -->
- <Services>
- <asp:ServiceReference Path="~/WebService/JsWebServiceTest.asmx"/>
- </Services>
- </asp:ScriptManager>
- <input type="button" value="查看" onclick="jsCallWeb();" />
- <br />
- <input type="button" value="查看2" onclick="jsCallWeb2();" />
- </div>
- </form>
- </body>
- </html>
- <script type="text/javascript">
- //调用1
- function jsCallWeb() {
- JsWebServiceTest.HelloWorld(outResult);
- }
- //调用2
- function jsCallWeb2() {
- JsWebServiceTest.WelCome('kevin', outResult);
- }
- //输出结果
- function outResult(result) {
- alert(result);
- }
- </script>