前臺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>
Webservice
頁面
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 "可以使用,請繼續";
}
}
}
}