ReCaptcha 2.0与AJAX

我已经设法让ReCaptcha 2.0在我的网站上工作。 但是,只有当我不使用AJAX,并让表单“自然”提交时才有效。

我想提交带有validation码的表单,并在不刷新页面的情况下提醒用户注意成功。

我尝试了下面的代码,但是好像服务器没有得到用户响应:

HTML:

<form class="form" action="javascript:void(0)" novalidate> <!-- all the inputs... --> <!-- captcha --> <div class="input-group"> <div class="g-recaptcha" data-sitekey="6LdOPgYTAAAAAE3ltWQGar80KUavaR-JblgPZjDI"></div> </div> <div class="errors" id="errors" style="display: none"></div> <div class="input-group"> <input type="button" value="Send" class="btn-default right" id="submit"> <div class="clear"></div> </div> </form> 

JS:

 $('#submit').click(function(e) { console.log('clicked submit'); // --> works var $errors = $('#errors'), $status = $('#status'), name = $('#name').val().replace(/<|>/g, ""), // prevent xss email = $('#email').val().replace(/<|>/g, ""), msg = $('#message').val().replace(/<|>/g, ""); if (name == '' || email == '' || msg == '') { valid = false; errors = "All fields are required."; } // pretty sure the problem is here console.log('captcha response: ' + grecaptcha.getResponse()); // --> captcha response: if (!errors) { // hide the errors $errors.slideUp(); // ajax to the php file to send the mail $.ajax({ type: "POST", url: "http://orenurbach.com/assets/sendmail.php", data: "email=" + email + "&name=" + name + "&msg=" + msg + "&g-recaptcha-response=" + grecaptcha.getResponse() }).done(function(status) { if (status == "ok") { // slide down the "ok" message to the user $status.text('Thanks! Your message has been sent, and I will contact you soon.'); $status.slideDown(); // clear the form fields $('#name').val(''); $('#email').val(''); $('#message').val(''); } }); } else { $errors.text(errors); $errors.slideDown(); } }); 

PHP:

 <?php // assemble the message from the POST fields // getting the captcha $captcha = ''; if (isset($_POST['g-recaptcha-response'])) $captcha = $_POST['g-recaptcha-response']; echo 'captcha: '.$captcha; if (!$captcha) echo 'The captcha has not been checked.'; // handling the captcha and checking if it's ok $secret = 'MY_SECRET'; $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true); var_dump($response); // if the captcha is cleared with google, send the mail and echo ok. if ($response['success'] != false) { // send the actual mail @mail($email_to, $subject, $finalMsg); // the echo goes back to the ajax, so the user can know if everything is ok echo 'ok'; } else { echo 'not ok'; } ?> 

PHP页面中的结果

 captcha: The captcha has not been checked.array(2) { ["success"]=> bool(false) ["error-codes"]=> array(1) { [0]=> string(22) "missing-input-response" } } not ok 

底线是,我怎么能手动获得input响应,没有它自动与POST数据的其余部分?

好吧,这非常愚蠢。

我做了一些错误的事情:

  • 在PHP文件中,所有的string都有单引号,这就导致了问题。
  • 在整个testing过程中,我在PHP文件中添加了多个东西的打印,因此if (status == "ok")从来没有工作。 我收到了电子邮件,但没有得到我所做的任何构想,现在我明白了为什么。
  • 当我想检查PHP文件是否省略时,我只是去了它在URL中的地址,并总是得到一个错误。 即使邮件被发送。 现在我明白,这不是检查日志的正确方法。

感谢@Samurai帮我弄清楚了事情。


最终的PHP代码:

 <?php // assemble the message from the POST fields // getting the captcha $captcha = ""; if (isset($_POST["g-recaptcha-response"])) $captcha = $_POST["g-recaptcha-response"]; if (!$captcha) echo "not ok"; // handling the captcha and checking if it's ok $secret = "MY_SECRET"; $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"]), true); // if the captcha is cleared with google, send the mail and echo ok. if ($response["success"] != false) { // send the actual mail @mail($email_to, $subject, $finalMsg); // the echo goes back to the ajax, so the user can know if everything is ok echo "ok"; } else { echo "not ok"; } ?>