web窗体------WebForm1.aspx
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="js/Jquery1.7.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#txtUserName').blur(function () { WebAjax.WebService1.ValidateUser($(this).val(),onSuccess,onFailed); }) function onSuccess(result) { $('#spinfo').text(result); } function onFailed() { $('#spinfo').text('在验证您的用户名是否可用时发生错误,请稍后再试,请反馈到某邮箱'); } }) </script> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="WebService1.asmx" /> </Services> </asp:ScriptManager> <div> 用户名<input type="text" id="txtUserName" /><span id="spinfo"></span><br /> 密码<input type="text" id="txtPwd" /> </div> </form> </body> </html>
wed服务---------------WebService1.asmx
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebAjax { /// <summary> /// WebService1 的摘要说明 /// </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 WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string ValidateUser(string username) { if (username == "onlifes") { return "用户名已被占用,请选择其他"; } else { return "可以使用,请继续"; } } [WebMethod] public string GetDate() { return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); } } }原文链接:https://www.f2er.com/ajax/166366.html