I have an ajax form:
<form id="my_form">
<input type="text" id="field1" />
<input type="submit" value="submit" />
</form>
And js code:
document.getElementById("my_form").onsubmit = function(e) {
e.preventDefault();
var xhr = new XMLHttpRequest();
//.............. send request to a server
In the documentation it assumes that a form is a normal form, not ajax. How exactly should I integrate invisible reCaptcha to my ajax forms? For example:
<form id="my_form">
<input type="text" id="field1" />
<div class="g-recaptcha" data-sitekey="12345" data-callback="????></div>
<input type="submit" value="submit" />
</form>
And, in particular, what should I specify for "data-callback" handler? Again, in the documentation it data-callback submits a form, but a normal form, whereas mine is ajax. Do I need "data-callback" at all? Shouldn't I instead call recaptcha inside my handler? How?
There're "render", "getResponse" and "execute". Which one should I use? It's not clear from the documentation.
You can easily validate google recaptcha's using jquery
I agree that "invisible" recaptcha documentation is not comprehensive enough. I had to spend some time digging thru code example and documentations of "visible" recaptcha before understanding how to work with this.
Let talk about the recaptcha API first:
grecaptcha.render(htmlEl, options, inherit)
is JS API method of rendering the captcha HTML on the page. By default recaptcha script will try to find any element withclass="g-recaptcha
and try to render immediately, but this behavior can be overridden by appending?render=explicit
query param to recaptcha script src url. You also may want to render the recaptcha html on demand using this api when your recaptcha.g-recaptcha
element gets attached to DOM at a point later than when script was loaded. This api returns an ID value that can be passed to other api methods, but if not passed, those api's lookup and reference first repcaptcha on page.grecaptcha.getResponse(optional_id)
returns the token. If token is empty string, it means user has not been validated yet i.e. user hasn't completed the captcha challenge.grecaptcha.execute(optional_id)
api triggers the recaptcha challenge on-demand programmatically. This api is only applicable to "invisible" recaptcha. Visible recaptcha challenges are triggered when user clicks the recaptcha module.grecaptcha.reset(optional_id)
will reset a challenge i.e. it must be done each time server fails to validate the token with recaptcha api server (because tokens are one time use), but depending on your implementation, you may decide to reset any time.Now, lets talk about data-callback:
data-callback
is an attribute where you can pass a name of global namespaced function, i.e. some function which is accessible as window['nameOfFunction']. This callback will get called each time user is successfully validated with a token value that you will eventually be passing to server. This is same token that is returned bygrecaptcha.getResponse()
so technically you do not need this function at all. But it can serve as callback to let you know user has passed verification in case you need to update UI or something.If for some reason you do not want this callback to be accessible from window namespace, you can pass this method in options object with
callback
key togrecaptcha.render()
. NOTE:options.callback
can take a string value which is equivalent to passingdata-callback
attribute in HTML, i.e. is must be a function in window namespace. Butoptions.callback
can take a "function" value as well.Now some sample code:
HTML
JS