客户端代码
<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
var verified = function() {
document.getElementById("loginform").submit();
};
</script>
<form action="www.example.com/" method="POST" id="loginform" onsubmit=" return validation()">
<input id="email" maxlength="80" name="email" size="20" type="text" placeholder="Enter Your Email" style="margin-bottom: 30px;"/><br>
<div id="captchadiv">
<div class="g-recaptcha" data-sitekey="site key" data-callback="verified"></div>
</div>
<button type="submit" value="Submit" id="reg_submit" style=" display:block;margin: 0 auto;"><img src="/favicon.png" style="width: 20px;float: left;" />Sign in</button>
</form>
Run Code Online (Sandbox Code Playgroud)
服务器端代码
reCAPTCHA=require('recaptcha2')
recaptcha=new reCAPTCHA({
siteKey:'site key',
secretKey:'secretKey'
})
Run Code Online (Sandbox Code Playgroud)
我正在研究node js。我正在使用谷歌recaptcha2,当我看到很多示例并且所有示例都recaptcha使用表单提交进行验证时。他们在操作中定义,但我的操作方法在其他导航中使用,因此我可以使用get, post请求。我不知道如何使用get, post请求。recaptcha我想recaptcha使用在服务器端进行验证get,post进行验证。
我需要后端验证工作的帮助。提前致谢!
请尝试这个代码
客户端代码不太安全,因此请使用两侧代码
客户端
function validateform(){
var captcha_response = grecaptcha.getResponse();
if(captcha_response.length == 0 || grecaptcha != undefined )
{
// Captcha is not Passed
return ' Please verify you are not a robot.';
}else{
$.get('/captchaTest',{'response':captcha_response},function(response){
if(response == undefined && response.responseCode == undefined && response.responseDesc == undefined && response.responseCode !== 0 && response.responseDesc !== 'Sucess' ){
return ' You are a robot.';
}
grecaptcha.reset();
});
}
}
Run Code Online (Sandbox Code Playgroud)
服务器端
app.get('/captchaTest',function(req,res){
var requestQuery = req.query;
if( requestQuery != undefined && requestQuery != '' && requestQuery != null && requestQuery.response != undefined && requestQuery.response != '' && requestQuery.response != null ){
var response = requestQuery.response;
var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret="+ secret_key +"&response=" +response;
// Hitting GET request to the URL, Google will respond with success or error scenario.
request(verificationUrl,function(error,response,body) {
body = JSON.parse(body);
// Success will be true or false depending upon captcha validation.
if(body.success !== undefined && !body.success) {
res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
}else{
res.send({"responseCode" : 0,"responseDesc" : "Sucess"});
}
});
}else{
res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6408 次 |
| 最近记录: |