ValidateUserName未定义

我有一个jquery ajax调用asp.net web服务问题。 我想validationasp.net网页中的文本框。 validation器是:

 

jquery代码是(第2次更新):

    $.ajax({ type: "POST", url: "UserNameWebService.asmx/ValidateUserName", data: "{'strUsername': " + $("#TextUserName").val() + "}", contentType: "application/json; charset=utf-8", dataType: "json" });  
General user information

Web服务代码是:

 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class UserNameWebService : System.Web.Services.WebService { [WebMethod] public bool ValidateUserName(string strUsername) { string UserNameCreated = strUsername; string AD_Server = System.Configuration.ConfigurationManager.AppSettings["AD_Server"]; DirectoryEntry entry = new DirectoryEntry(AD_Server); entry.AuthenticationType = AuthenticationTypes.Secure; DirectorySearcher deSearch = new DirectorySearcher(entry); deSearch.Filter = "(&(objectClass=user)(samaccountname=" + UserNameCreated + "))"; SearchResultCollection results = deSearch.FindAll(); Match match = Regex.Match(UserNameCreated, @"^[a-zA-Z0-9]{6,}$", RegexOptions.IgnoreCase); if (results.Count > 0) return false; else if (match.Success) return true; else return false; } } 

但是我收到了一个错误:

 ValidateUserName is undefined. 

请帮我纠正错误。

非常感谢你!

您的代码实际上存在一些问题。

1) $("#TextUserName")在此上下文中不起作用,因为asp.net将使用不同的ID呈现服务器端TextBox控件。 你需要这样做:

 data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}", 

2)您的数据属性中的json格式不正确,您需要在这个值周围使用单引号:

  | | VV data: "{'strUsername': '" + $("#<%=TextUserName.ClientID%>").val() + "'}", 

3)您需要将Jquery ajax调用放在函数中,在您的情况下称为ValidateUserName 。 它需要两个参数sourceargs 。 此函数的职责是将args.IsValid的值设置为truefalse 。 因此,当ajax调用成功执行此逻辑时,您需要提供要调用的函数。 像这样:

 function ValidateUserName(source, args) { $.ajax({ type: "POST", url: "UserNameWebService.asmx/ValidateUserName", data: "{'strUsername': '" + args.Value + "'}", contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function (result) { args.IsValid = result.d; } }); } 

4)正如您在上面的代码中看到的,您实际上不需要使用jquery从文本框中获取值,因为您可以像这样args.Value访问它。

5)您需要添加async: false否则在设置IsValid ,设置消息可见性的代码将已经执行,因此不会发生任何事情。

您需要在函数调用ValidateUserName中包含ajax函数,以便它与ClientValidationFunction匹配。

 function ValidateUserName(){ $.ajax({ type: "POST", url: "UserNameWebService.asmx/ValidateUserName", data: "{'strUsername': " + $("#TextUserName").val() + "}", contentType: "application/json; charset=utf-8", dataType: "json" }); } 

即使这样,这也不会返回一个值。 我想你需要使用$.ajax函数的success选项来设置一个布尔值来返回函数。

所以你会做类似的事情:

 function ValidateUserName(){ var isValid; $.ajax({ type: "POST", url: "UserNameWebService.asmx/ValidateUserName", data: "{'strUsername': '" + $("#TextUserName").val() + "'}", contentType: "application/json; charset=utf-8", dataType: "json" , async: false , success: function(data){ isValid = data; } }); return isValid; } 

注意我也将async设置为false。