jQuery、自定义验证和 AJAX
我的问题更多的是基于流程而不是基于代码,但为了分享(可能会偶然发现一些不一致之处),我正在使用:
- jQuery 1.6.2
- jQuery Validations 1.8.1
所以这里...
添加自定义验证时(使用 addMethod)如果需要调用服务器(而不是内置的远程调用),则必须将响应设置为同步发生,因为该方法将在结果从服务器返回之前完成。这是正确的吗?
我还假设执行此操作的两种主要方法是通过使用 async false 或 deferred.promise() 调用 $.ajax 请求。这是正确的吗?
我一直在浏览许多帖子,询问与使用 getJSON 进行自定义验证相关的问题,并且回复通常是“您不会返回任何内容”,但这是否没有抓住要点 - 回复是否也太晚了?
感谢您的任何回复!
My question is more process based than code based, but to share (and maybe stumble upon some inconsistencies) I am using:
- jQuery 1.6.2
- jQuery Validations 1.8.1
So here goes...
When adding in a custom validation (using addMethod) that requires a call to the server (instead of the remote call built in), you must set to response to happen synchronously because the method will finish before the result returns from the server. Is this correct?
I'm also assuming the 2 main ways to do this are via calling an $.ajax request with async false, or deferred.promise(). Is this correct?
I've been looking thru many posts that ask questions related to custom validation using getJSON and the responses are usually "you're not returning anything back" but doesn't this miss the point - isn't the response too late as well?
Thanks for any responses!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的假设是正确的。您不能将异步请求与自定义方法一起使用,因为 jQuery 验证需要知道这是一个异步请求。这就是为什么您应该在此使用
remote
规则情况而不是自定义方法:我不建议您使用同步 AJAX 请求(或 SJAX)。
Your assumptions are correct. You cannot use asynchronous requests with custom methods because the jQuery validate needs to know that this is an async request. That's why you should use the
remote
rule in this situation and not custom methods:Synchronous AJAX requests (or SJAX) are not something that I would recommend you doing.