问题
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. 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).
Best regards
回答1:
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....
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.
来源:https://stackoverflow.com/questions/42352147/custom-parsley-js-asynchronous-validator-loops-twice