Google ReCaptcha不会返回错误

时间:2016-02-17 13:35:30

标签: php ajax laravel-5.1 recaptcha

我正在尝试通过AJAX发送recaptcha密钥,一切似乎都没问题,但由于某种原因,php recaptcha sdk不会返回错误,但响应不成功。

这是我的PHP代码:

public static ulong? SumOrNull<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, int> projection)
{
    bool any = false;

    ulong sum = 0;

    foreach (var item in source)
    {
        any = true;

        //As commented by CodesInChaos,
        //we use the checked keyword to make sure that
        //we throw an exception if there are any negative numbers
        sum = sum + (ulong)checked((uint)projection(item));
    }

    if (!any)
        return null;

    return sum;
}

Ajax请求:

$recaptcha = new \ReCaptcha\ReCaptcha($user->secret);
        $resp = $recaptcha->verify($data['g-recaptcha-response'], request()->ip());

    if ($resp->isSuccess()) {
        //Success
    }else{
        return response()->json([
            'message' => 'Recaptcha error',
            'errors' => $resp->getErrorCodes(),
        ], 401);
    }

回复:

$.ajax({
    type: 'POST',
    url: 'http://localhost/endpoint',
    datatype: 'json',
    cache: false,
    data: {
        email: document.getElementById('register_email').value,
        password: document.getElementById('register_password').value,
        password_confirmation:document.getElementById('register_re_password').value,
        first_name:document.getElementById('register_first').value,
        last_name:document.getElementById('register_last').value,
        'g-recaptcha-response': grecaptcha.getResponse(widgetId2)
    },
    success: function(result) {
        window.alert('Success');
        grecaptcha.reset(widgetId2);
    },
    error: function(result) {
        window.alert(result.responseJSON);
    }
});

这个秘密正在发挥作用,因为如果我把无效的g-recaptcha-response抛出正确的错误,如果我改变了秘密就会发生同样的错误。

1 个答案:

答案 0 :(得分:0)

问题是一个糟糕的网站密钥,由于某些原因,SDK没有解决此问题。

如果有人遇到类似问题,请检查网站和密钥是否完整,并从ReCaptcha管理面板中复制完好。