gpt4 book ai didi

php - Recaptcha 出现 Ajax 错误

转载 作者:行者123 更新时间:2023-12-01 04:56:46 26 4
gpt4 key购买 nike

我在验证验证码输入时遇到问题。这是我的代码:

// 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 语句中不起作用?

最佳答案

您正在向服务器发出异步请求,这意味着当 $.ajax() 行完成并继续执行 console.log() 和 if() 语句时,对服务器的实际请求仍处于挂起状态并且尚未完成。在执行 if() 语句时,responseText 实际上是“未定义”。

相反,您需要使用“成功”回调函数,如下所示:

// 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;
}
}
});

关于php - Recaptcha 出现 Ajax 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13845850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com