Last active
December 16, 2024 03:26
-
-
Save jiankaiwang/dc5690318bd035232a8ac9294bc2af29 to your computer and use it in GitHub Desktop.
the example to use recaptcha in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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> |
Thanks!
Thank you!!!
thank you so much.. this was very useful for me!
Thanks it is really useful !
Awesome and up to the point
thank you!!
working.
thank you!!
Anyone Help Me Please Contract
[email protected]
Is this for Recaptcha v3? I am getting this error "ERROR for site owner: Invalid key type"
It's great, do I have to use the Gmail to get my own site key for Recaptcha?
working for me
thank you!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice piece of code thanks