我无法验证重新接收输入。继承我的代码:
// Validate Recaptcha Input
var challenge = $("#recaptcha_challenge_field").val();
var response = $("#recaptcha_response_field").val();
var dataString = 'recaptcha_challenge_field=' + challenge + '&recaptcha_response_field=' + response;
var html = $.ajax({
type: "POST",
url: "PHP/recaptchaverify.php",
data: dataString,
async: true
}).responseText;
console.log(html);
if(html == 'accept')
{
alert("CORRECT");
}
else
{
alert("The reCAPTCHA wasn't entered correctly. Go back and try it again.");
$("#recaptcha_response_field").focus();
Recaptcha.reload();
return false;
}
现在我将我的变量传递给recpatchaverify.php
require_once('../scripts/recaptcha-php/recaptchalib.php');
$privatekey = "MYKEY";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
// What happens when the CAPTCHA was entered incorrectly
echo "error";
}
else
{
// Your code here to handle a successful verification
echo "accept";
}
现在我的问题是每当我正确输入Recaptcha时html变量都会显示“accept”,但它在IF语句中不起作用?
答案 0 :(得分:1)
您正在向服务器发出异步请求,这意味着当$ .ajax()行完成并继续执行到console.log()和if()语句时,对服务器的实际请求仍处于未决状态还没有完成。在if()语句执行时,responseText实际上是'undefined'。
相反,您需要使用'success'回调函数,如下所示:
// Validate Recaptcha Input
var challenge = $("#recaptcha_challenge_field").val();
var response = $("#recaptcha_response_field").val();
var dataString = 'recaptcha_challenge_field=' + challenge + '&recaptcha_response_field=' + response;
$.ajax({
type: "POST",
url: "PHP/recaptchaverify.php",
data: dataString,
success: function(html) {
if(html == 'accept')
{
alert("CORRECT");
}
else
{
alert("The reCAPTCHA wasn't entered correctly. Go back and try it again.");
$("#recaptcha_response_field").focus();
Recaptcha.reload();
return false;
}
}
});