$_POST["g-recaptcha-response"],它存储在 Captcha div 元素中的什么位置?如何使用 AJAX 处理 Recaptcha?

$_POST["g-recaptcha-response"], where is it stored in the Captcha div element? How to handle Recaptcha with AJAX?

我有一个简单的表单,它向通过电子邮件发送消息的脚本发送 AJAX 调用。我以前只有一个简单的表单提交并且工作正常,但是当过渡到使用 AJAX,$_POST 参数之一,'g-recaptcha-response',我似乎无法抓住。

<form action="./scripts/sendEmail.php" id="emailform" method="post">
    <textarea id="mess" rows="10" cols="50" name="message"></textarea> <br/><br/>
    <div class="g-recaptcha" data-sitekey="..."></div> <br/>
    <input type="submit" value="Send"/>
</form>

sendEmail.php 接受两个 $_POST 值:'message''g-recaptcha-response'。 AJAX如下:

$(document).ready(function() {
    $('#emailform').submit(function(event) {
        event.preventDefault();
        var $form = $(this);
        $.ajax({
            url: $form.attr('action'),
            method: $form.attr('method'),
            dataType: 'json',
            data: {
                message: $('textarea#mess').val(),
                g-recaptcha-response: //???
            }
        });
    });
});

我已经为注释 //??? 尝试了几个值,例如 $('input#recaptcha-token').val(),因为 sendEmail.php 上的 $_POST['g-recaptcha-response'] 匹配此标记,但我仍然得到提交时出错。

Notice: Use of undefined constant success - assumed 'success' in dir\sendEmail.php

sendEmail.php 如下所示:

if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
}
if ($captcha) {
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=/*secret*/&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
    if ($response.success == true) {
        //send e-mail
        exit;
    }
}
//error handling

我不确定我的问题是在抓取 $_POST['g-recaptcha-response'] 还是其他问题。将值更改为 "" 会弹出相同的错误。

我想重申一下,没有 AJAX 一切正常。我没有收到错误消息,response.success 得到了应有的处理。

我明白了。 g-recaptcha-respone 存储在文本区域 (textarea#g-recaptcha-response) 中,我之前尝试过但没有成功。事实证明这是 JQuery 中的错误:g-recaptcha-response:$('textarea#g-recaptcha-response').val() 不起作用,因为 $_POST 变量需要没有连字符 (???)。用引号括起来让它工作,一切都很顺利。