自定义Parsley.js异步验证程序循环两次 [英] Custom Parsley.js asynchronous validator loops twice

查看:137
本文介绍了自定义Parsley.js异步验证程序循环两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个下午我一直在解决这个问题,我似乎无法调试它.问题是,当我执行页面的硬刷新(文档就绪触发)时,我的自定义异步验证器运行两次,但仅向服务器发布一次.

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');

基本上,它将在控制台中打印两次Runs,但不会重复服务器发布.每次循环时,它还会打印一条错误消息(重复).如果我从链接进入页面,则不会发生这种情况(使用smoothState和Ajax dom插入,因此不会触发准备就绪的文档).提交表单位于页面底部,没有包装成文档就绪状态,验证器也不位于单独的.js文件中(也没有包装成文档就绪状态).仅使用此自定义验证程序(这也是唯一一个使用ajax的验证程序)时,才会发生此问题.

Basically it's gonna print Runs twice in the console, but it's not gonna repeat the server post. 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). 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). This problem only occurs with this custom validator (which is also the only one that is an ajax one).

最诚挚的问候

推荐答案

现在没有时间检查源,但完全有可能每个请求验证器运行一次以上.特别是,ajax请求被缓存了...

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.

这篇关于自定义Parsley.js异步验证程序循环两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆