Html:
代碼
<ASP:ScriptManager runat="server" ID="sm">
<Services>
<ASP:ServiceReference Path="~/validateUserName.asmx" />
</Services>
</ASP:ScriptManager>
<div>
<input type="text" id="userName" onchange="test();"/>
<span id="msg"></span>
</div>
JS:
代碼
<script type="text/Javascript">
function test() {
requestService = JQueryProj.validateUserName.GetUser(document.getElementById("userName").value, validate);
}
function validate(result) {
if (result == "false") {
msg.innerHtml = "用戶名已存在!";
}
else {
msg.innerHtml = "用戶名可用!";
}
}
</script>
Web Service
代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
namespace JQueryProj
{
/// <summary>
/// validateUserName 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.Net AJax 從腳本中調用此 Web 服務,請取消對下行的注釋。
[ScriptService]
public class validateUserName : System.Web.Services.WebService
{
[WebMethod]
public string GetUser(string userName)
{
if (userName == "admin")
{
return false.ToString().ToLower();
}
else
{
return true.ToString().ToLower();
}
}
}
}