I use reCaptcha+validation jQuery plugin:
<script>
$('#contact_us-form').validate({
submitHandler: function(form) {
grecaptcha.execute(); //How to run this syncly?
$(form).ajaxSubmit({ ... }); // This request without token
}
});
</script>
<form>
...
<div class='g-recaptcha' ... />
</form>
This code almost works. Almost because execute
is run async and response is come after ajaxSubmit
submits form data.
The work around is to assign callback
property for g-recaptcha
and move ajaxSubmit
into that callback:
var my_callback = function() {
$(form).ajaxSubmit({ ... });
}
<div class='g-recaptcha' data-callback='my_callback'/>
But this looks hairly. Furthermore the form
variable is not available from my_callback
thus I can not reuse this call back between similar forms.
Is there a way to execute
synchronously?