I am rendering the google recaptcha widget in my jsp page and programmatically invoking the challenge in my java script when I submit the form.
<script type="text/javascript" id="recaptcha-response">
var siteKey = $('#recaptchasitekey').first().text();
var onloadCallback = function() {
grecaptcha.render('recaptcha_element', {
'sitekey' : siteKey,
'callback' : correctCaptcha,
'data-bind' : "qoActionTemplate"
},true);
};
var correctCaptcha = function(response) {
return response;
};
</script>
<div class="g-recaptcha" id="recaptcha_element" data-size="invisible" ></div>
<script src="http://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
<script type="text/template" id="recaptchaUrl">
Here is my java Script code that invokes the challenge.
executeRecaptcha : function() {
grecaptcha.execute();
captcha.token = grecaptcha.getResponse();
if (captcha.token == null || captcha.token == '') {
grecaptcha.reset();
grecaptcha.execute();
}
return captcha.token;
},
validateRecaptcha : function(response,checkToken) {
captcha.executeRecaptcha();
if (captcha.token) {
var captchaUrl = $('#recaptchaUrl').first().text();
captcha.userSIDVerify = $('#captcha_token').val();
$.ajax({
type : 'POST',
url : captchaUrl,
data : {
response : captcha.token
},
success : captcha.checkToken,
error : function() {
alert("failed")
return;
}
});
}
}
My grecaptcha.getResponse()
is always empty on my first form submission and then for the second time I submit I get a response.