Ajax的reCaptcha问题 [英] reCaptcha Issues with Ajax

查看:130
本文介绍了Ajax的reCaptcha问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现reCaptcha,并且正在使用Ajax调用我的PHP页面来检查验证码的有效性,而无需刷新页面.

I'm implementing reCaptcha, and I'm using an Ajax call to a PHP page of mine to check the validity of the captcha, without a page refresh.

我有这个jQuery代码:

I have this jQuery code:

$.post('php/captcha.php', $('#captchaPost').serialize(), function(data){
            if(data != "Valid")
            {
                $('#captchaError').show();
                $captchaFlag = "Invalid";
            }
            else
            {
                $('#captchaError').hide();
                $captchaFlag = "Valid";
            }
        });

此PHP代码用于后处理程序:

And this PHP code for the post handler:

<?php
require_once('recaptchalib.php');
$privatekey = "1234567890";
$resp = recaptcha_check_answer ($privatekey,
                        $_SERVER["REMOTE_ADDR"],
                        $_REQUEST["recaptcha_challenge_field"],
                        $_REQUEST["recaptcha_response_field"]);

if (!$resp->is_valid) 
{
// What happens when the CAPTCHA was entered incorrectly
echo "Error";
} 

else 
{
echo "Valid";
}
?>

我使用Firebug检查了响应,即使输入正确的验证码,PHP脚本也始终返回错误".根据服务器,该表单似乎可以正确发布,尽管我看不到如何检查表单中发布的内容.我没有使用PHP函数来构建reCaptcha表单;我从Google的文档中获得了HTML.有帮助吗?

I checked the response using Firebug and the PHP script always returns "Error", even when I type in the correct Captcha. The form seems to POST correctly, according to the server, although I don't see how to check what was posted in the form. I am not using the PHP function to build the reCaptcha form; I got the HTML from Google's docs on this. Any help?

推荐答案

尝试使用此代码:

function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    var html = $.ajax({
    type: "POST",
    url: "php/captcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&amp;recaptcha_response_field=" + responseField,
    async: false
    }).responseText;

    if(html != "Valid")
    {
        $('#captchaError').show();
        $captchaFlag = "Invalid";
    }
    else
    {
        $('#captchaError').hide();
        $captchaFlag = "Valid";
    }
}

看起来您不是在jQuery中正确发送了数据.

It doesn't look like you were sending the data correctly in your jQuery.

修改 还要确保在按钮上调用validateCaptcha().例如:

Edit Also make sure to call validateCaptcha() on the button. For instance:

onSubmit="javascript:validateCaptcha()"

这篇关于Ajax的reCaptcha问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆