我在我的网站上一起使用PHP和AJAX来从JSON URL获取数据并在网页上显示它。当我使用它而不实现recaptcha时,它工作正常,但是当我整合Google的Recaptcha时,只有当验证码拼图每次解决两次时才会显示结果。我不确定这个bug究竟在哪里,我甚至试图实现自定义验证码,在这种情况下它也是一样的。这是带有recaptcha的代码,
Captcha和Ajax代码段:
<?php
if ($resp != null && $resp->success): ?>
echo"hi";
<script>
$(document).ready(function(){
$("#submit").click(function(){
$.post("retrieve_train_between_stations.php", $("#get_train_running").serialize(), function(response) {
$("#success").html(response);
});
return false;
});
});
</script>
<?php
else:
echo "Failed";
?>
答案 0 :(得分:1)
此部分应移至retrieve_train_between_stations.php
。
require_once "recaptchalib.php";
// your secret key
$secret = "My secret key";
// check secret key
$reCaptcha = new ReCaptcha($secret);
$resp = false;
if (isset($_POST["g-recaptcha-response"])) {
$resp = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
if ($resp) {
//display the record
} else {
echo 'Recaptcha can not be verified.';
}
应删除if / else并阻止脚本的默认事件
<script>
$(document).ready(function(){
$("#submit").click(function(event){
event.preventDefault();
$.post("retrieve_train_between_stations.php", $("#get_train_running").serialize(), function(response) {
$("#success").html(response);
});
return false;
});
});
</script>