Skip to content

Instantly share code, notes, and snippets.

@jiankaiwang
Last active December 16, 2024 03:26
Show Gist options
  • Select an option

  • Save jiankaiwang/dc5690318bd035232a8ac9294bc2af29 to your computer and use it in GitHub Desktop.

Select an option

Save jiankaiwang/dc5690318bd035232a8ac9294bc2af29 to your computer and use it in GitHub Desktop.
the example to use recaptcha in javascript
<!--
Description: The following is the example code to use recaptcha.
Notice: The function backend_API_challenge is the concept and you should implement it on the backend.
Especially, you must keep your secret key in private all the time.
Flow:
1. Click the submit button.
2. On the console, execute backend_API_challenge function.
-->
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
</head>
<body>
<form>
<input type="text"></input>
<div class="g-recaptcha" data-sitekey="(your-site-key)"></div>
<input type="button" onclick="checkRecaptcha();" value="submit"></input>
</form>
<script>
function checkRecaptcha() {
var response = grecaptcha.getResponse();
if(response.length == 0) {
//reCaptcha not verified
alert("no pass");
}
else {
//reCaptch verified
alert("pass");
}
}
// implement on the backend
function backend_API_challenge() {
var response = grecaptcha.getResponse();
$.ajax({
type: "POST",
url: 'https://www.google.com/recaptcha/api/siteverify',
data: {"secret" : "(your-secret-key)", "response" : response, "remoteip":"localhost"},
contentType: 'application/x-www-form-urlencoded',
success: function(data) { console.log(data); }
});
}
</script>
</body>
</html>
@dglite

dglite commented Aug 27, 2019

Copy link
Copy Markdown

this code was REALLY useful for me. Thks a lot!!!

@yaqizhangIress

Copy link
Copy Markdown

We tried this method it didn't work and we got failed to execute 'send' on 'xmlhttprequest': failed to load. any idea? Thanks

@aathuat

aathuat commented Dec 3, 2019

Copy link
Copy Markdown

Like GOod

@Ruh444-svg

Copy link
Copy Markdown

@healthdev2020

Copy link
Copy Markdown

Very nice piece of code thanks

@scoutfrc

Copy link
Copy Markdown

Thanks!

@thecoolwinter

Copy link
Copy Markdown

Thank you!!!

@johannesschobel

Copy link
Copy Markdown

thank you so much.. this was very useful for me!

@gabrielctn

Copy link
Copy Markdown

Thanks it is really useful !

@wam090

wam090 commented Feb 3, 2023

Copy link
Copy Markdown

Awesome and up to the point
thank you!!

@Arulselvi97

Copy link
Copy Markdown

working.
thank you!!

@SjSohag

SjSohag commented Oct 9, 2023

Copy link
Copy Markdown

Anyone Help Me Please Contract
Dreambd71onlinezoon@gmail.com

@muhammadzubairsultan

Copy link
Copy Markdown

Is this for Recaptcha v3? I am getting this error "ERROR for site owner: Invalid key type"

@YSHgroup

YSHgroup commented Jun 3, 2024

Copy link
Copy Markdown

It's great, do I have to use the Gmail to get my own site key for Recaptcha?

@aabib

aabib commented Oct 8, 2024

Copy link
Copy Markdown

working for me
thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment