Skip to content

Instantly share code, notes, and snippets.

@jiankaiwang
Last active March 25, 2024 09:39
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jiankaiwang/dc5690318bd035232a8ac9294bc2af29 to your computer and use it in GitHub Desktop.
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
Copy link

dglite commented Aug 27, 2019

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

@yaqizhangIress
Copy link

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

@aathuat
Copy link

aathuat commented Dec 3, 2019

Like GOod

@Ruh444-svg
Copy link

@healthdev2020
Copy link

Very nice piece of code thanks

@scoutfrc
Copy link

Thanks!

@thecoolwinter
Copy link

Thank you!!!

@johannesschobel
Copy link

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

@gabrielctn
Copy link

Thanks it is really useful !

@wam090
Copy link

wam090 commented Feb 3, 2023

Awesome and up to the point
thank you!!

@Arulselvi97
Copy link

working.
thank you!!

@SjSohag
Copy link

SjSohag commented Oct 9, 2023

Anyone Help Me Please Contract
Dreambd71onlinezoon@gmail.com

@muhammadzubairsultan
Copy link

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

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