<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestHelloWorld.aspx.cs" Inherits="AjaxWebservice.TestHelloWorld" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="sm1" runat="server"> <Services> <asp:ServiceReference Path="~/selfWS.asmx" /> </Services> </asp:ScriptManager> </div> <script type="text/javascript"> AjaxWebservice.selfWS.HelloWorld(OnRequestComplete); function OnRequestComplete(result) { alert(result); } </script> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace AjaxWebservice { /// <summary> /// selfWS 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 [System.Web.Script.Services.ScriptService] public class selfWS : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } }