如何validation服务器调用中的recaptcha?

情况:

我曾经用POST提交一个简单的表单来检查我的recaptcha“/ login”。

我需要改变我的实现出于安全原因,并希望做一些事情:

1)jquery表单提交。

2)打电话给服务器调用服务器上的validationrecaptcha。

3)接收响应,无需重新加载页面。

4)接受login请求或不响应。


题:

看起来我可以做一个AJAX请求? 但我不知道如何。


客户代码:

<div class ="containerMargins"> <h1 class = "authTitle">Login</h1> <form id="loginForm"> <div class="form-group"> <label>Email</label> <input type="email" class="form-control" name="email" id="loginEmail" placeholder="You can't forget it :)" required> </div> <div class="form-group"> <label>Password</label> <input type="password" class="form-control" name="password" id="loginPassword" placeholder="We hope you didn't forget it ^^" required minlength="12"> </div> <div class="g-recaptcha" data-sitekey="6LcRrxMUAAAAANx-AXSdRLAo4Pyqfqg-1vuPSJ5c"></div> <button class="btn btn-default" id="loginButton">Submit</button> <span class="userLinks"> <a class="logLinks" href="/users/register">Register</a><a href="/users/password">Password?</a></span> </form> </div> </div> <% include ../partials/indexScripts %> <script> $("#loginForm").submit(function(e) { e.preventDefault(); var email = $("#loginEmail").val(); var password = $("#loginPassword").val(); // WOULD LIKE TO MAKE SERVER CALL HERE TO CHECK RECAPTCHA BUT I DONT KNOW HOW $this = $(this); $.ajax({ type: "POST", url: "users/register", data: $this.serialize() }).done(function(data) { if (successfulRecaptcha) { firebase.auth().signInWithEmailAndPassword(email, password ).then( authData => { } else { console.log("You are a robot!"); } 

服务器代码:

 router.post('/login', function(req, res, next) { verifyRecaptcha(req.body["g-recaptcha-response"], function(success) { if (success) { } else { } }); }); 

我find了解决办法:

前端:

 $this = $(this); $.ajax({ type: "POST", url: "login", data: $this.serialize() }).done(function(data) { if (data == true) { 

BACKEND:

 router.post('/login', function(req, res, next) { verifyRecaptcha(req.body["g-recaptcha-response"], function(success) { if (success) { res.send(true); } else { res.send(false); } }); });