简体   繁体   English

自定义Parsley.js异步验证程序循环两次

[英]Custom Parsley.js asynchronous validator loops twice

I have been figuring this out for the whole afternoon and I can't seem to be able to debug this. 我整个下午一直在想办法,但似乎无法调试。 The problem is that when I do a hard refresh of the page (document ready fires) my custom async validator runs twice but only posts one time to the server. 问题是,当我执行页面的硬刷新(文档就绪触发)时,我的自定义异步验证器运行两次,但仅向服务器发布一次。

window.Parsley.addAsyncValidator('emailvalidation', function (data) {
  console.log("Runs");

  var myResponseText = data.responseText;
  var obj = jQuery.parseJSON(myResponseText);

  valid = (obj.result == 1);
  return valid;
}, '/check.do?action=userEmailAvailable&ajax=1');

Basically it's gonna print Runs twice in the console, but it's not gonna repeat the server post. 基本上,它将在控制台中Runs两次,但不会重复服务器发布。 For each time it loops it also prints one more error message (duplicate). 每次循环时,它还会打印一条错误消息(重复)。 This does not happen if I come to the page from a link (using smoothState and ajax dom insertion so document ready doesn't fire). 如果我从链接进入页面,则不会发生这种情况(使用smoothState和Ajax dom插入,因此不会触发准备就绪的文档)。 The submit form is at the bottom of the page and is not wrapped into document ready and neither is the validator which is in a separate .js file (also not wrapped in document ready). 提交表单位于页面底部,没有包装成文档就绪状态,验证器也不位于单独的.js文件中(也没有包装成文档就绪状态)。 This problem only occurs with this custom validator (which is also the only one that is an ajax one). 仅使用此自定义验证程序(这也是唯一一个使用ajax的验证程序)才会发生此问题。

Best regards 最好的祝福

No time to check the source right now, but it's entirely possible the validator will be run more than once per request. 现在没有时间检查源,但是完全有可能每个请求验证器运行一次以上。 In particular, ajax requests are be cached.... 特别地,ajax请求被缓存。

This should not be an issue though, your code is simply a translation from the ajax response to the end result (is it valid), running it multiple times should not have any impact. 不过,这应该不成问题,您的代码只是从ajax响应到最终结果(是否有效)的转换,多次运行它不会产生任何影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM