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
。 它需要两个参数source
和args
。 此函数的职责是将args.IsValid
的值设置为true
或false
。 因此,当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。
- 转储文件时,iframe onLoad不会触发?
- 使用ASP.net MVC4中的Modal弹出窗口添加/编辑/删除
- 此方案的Web窗体表格控件
- Jquery Ajax Call不调用.CS文件中的函数
- 从DB By Jquery-ajax获取
- 不使用Zip文件下载多个文件
- 获取突出显示的文本
- 使用JQuery从viewstate获取值?
- 无法从gridview中的文本框中获取修改后的值
Interesting Posts
如何在asp.net gridview中使用jquery tablesorter?
如何使用户只在复选框列表中选中一个复选框
替换outerHTML后,jQuery选项卡无法正常工作
如何在页面导航后跟踪手风琴(在母版页中)打开状态?
如何在Javascript中获取相对路径?
Webservice仅从JQuery调用返回内部错误500
如何使用JQueryvalidationemailid的文本框?
仅使用keyup事件触发表单validation中的文本框的一些规则,并取消其余部分的事件
POST Json没有模型和Ajax
Ajax Calender Extender无法在Asp.net Web应用程序中运行