Google reCaptcha回复"Uncaught(in promise)null"

FAB*_*BRj 33 html javascript php ajax recaptcha

我使用reCaptcha v2但在Uncaught (in promise) null任何情况下都在开发控制台响应(并移动.reset()函数)

安慰:

在此输入图像描述

我的recaptcha代码:

<div class="text-xs-center" style="text-align: center; height:150px;">
    <p style="color: black;"> Complete the verification: </p>
    <div style="display: inline-block;" class="g-recaptcha" data-sitekey="xxxxxxxxxxx" data-callback="callback"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

我的回调函数:

function callback() {
    if (grecaptcha === undefined) {
        alert('Recaptcha non definito'); 
        return; 
    }

    var response = grecaptcha.getResponse();
    console.log(response);

    if (!response) {
        alert('Coud not get recaptcha response'); 
        return; 
    }

    $.ajax({
    'url' : 'validate-recaptcha.php',
    'type' : 'POST',
    'data' : {
        'response' : response   
    },
    'success' : function(data) {              
        alert('Data: '+data);
    },
    'error' : function(request,error)
    {
        alert("Request: "+JSON.stringify(request));
    }
    });
    grecaptcha.reset();
}
Run Code Online (Sandbox Code Playgroud)

和我的validate-recaptcha.php:

<?php
//debug
$fp = fopen('debug.txt', 'a');
fwrite($fp, print_r($_POST, TRUE));
fclose($fp);
//enddebug

if (empty($_POST['recaptcha'])) {
    exit('Please set recaptcha variable');
}
// validate recaptcha
$response = $_POST['recaptcha'];
$post = http_build_query(
    array (
        'response' => $response,
        'secret' => 'yoursecretkey',
        'remoteip' => $_SERVER['REMOTE_ADDR']
    )
);
$opts = array('http' => 
    array (
        'method' => 'POST',
        'header' => 'application/x-www-form-urlencoded',
        'content' => $post
    )
);
$context = stream_context_create($opts);
$serverResponse = @file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
if (!$serverResponse) {
    exit('Failed to validate Recaptcha');
}
$result = json_decode($serverResponse);
if (!$result -> success) {
    exit('Invalid Recaptcha');
}
exit('Recaptcha Validated');
Run Code Online (Sandbox Code Playgroud)

在互联网上搜索,问题可能是.reset()功能,但我不明白解决方案.

Esa*_*fan 20

Recaptcha v2回调js错误

我也有这个错误,我发现与recaptcha回调有关(在你的情况下data-callback="callback").如果删除数据回调属性,则不会出现错误.

控制台错误Uncaught (in promise) null表示回调正在等待承诺.这是使用promises进行recaptcha的基本回调函数:

function callback() {
    return new Promise(function(resolve, reject) { 

    //Your code logic goes here

    //Instead of using 'return false', use reject()
    //Instead of using 'return' / 'return true', use resolve()
    resolve();

  }); //end promise
};
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您需要将代码调整为以下内容:

function callback() {
  return new Promise(function(resolve, reject) {  

    if (grecaptcha === undefined) {
        alert('Recaptcha non definito'); 
        //return;
        reject();
    }

    var response = grecaptcha.getResponse();
    console.log(response);

    if (!response) {
        alert('Coud not get recaptcha response'); 
        //return;
        reject();
    }

    $.ajax({
    'url' : 'validate-recaptcha.php',
    'type' : 'POST',
    'data' : {
        'response' : response   
    },
    'success' : function(data) {              
        alert('Data: '+data);
        resolve();
    },
    'error' : function(request,error)
    {
        alert("Request: "+JSON.stringify(request));
        reject();   
    }
    });
    grecaptcha.reset();

  }); //end promise
}
Run Code Online (Sandbox Code Playgroud)

这是我在SO中的第一个答案,所以请耐心等待,如果我忘记或错过了什么,请告诉我:)

  • 我在这里遇到了同样的问题,但我没有回调开始……但是有一天开始出现此错误……为了后代的缘故:事实证明,当网站未在“注册”网站时也会发生Google recaptcha / admin域区域。30分钟后突然恢复工作,原来我不得不将www.mysite,dev.mysite和mysite添加为3个单独的允许域。(我尚未添加dev子域,并且* .mydomain通配符不起作用)所以...可能想再次检查管理员设置。我也有htaccess密码保护我的域,也将其关闭了... (2认同)

Eli*_*ant 17

@ChristianŽagarskas在评论中指出:

原来,当网站未在Google Recaptcha / admin域区域中“注册”时,也会发生这种情况。

从开发密钥切换到生产密钥时,我犯了这个错误。生产密钥没有本地主机的任何条目。

我将API响应配置为位于代理重定向之后。因此,验证是在未在Google管理控制台中配置的localhost环境下进行的,导致此一般错误。

如果您的验证位于代理重定向的后面,请解决此错误的步骤:

  1. 登录已注册验证码的Google帐户
  2. 输入Google“ google recpatcha管理控制台”
  3. 转到(生产)键的设置
  4. 在“域”中,添加以下两个条目:
localhost
127.0.0.1
Run Code Online (Sandbox Code Playgroud)
  1. 保存并测试您的Recaptcha。

  • 这就是我的案例的答案。我正在本地测试,因此需要将 `localhost` 添加到 recaptcha (v3) 配置中 (3认同)

Joh*_*Rix 11

困扰我的这个错误的另一个触发因素是表单中的按钮的名称属性为“ submit”。如果使用reCaptcha文档中自动绑定示例代码,这将使其跳闸,因为“ form.submit”将引用按钮,而不是表单本身的commit()函数。h!

<html>
  <head>
    <title>reCAPTCHA demo: Simple page</title>
     <script src="https://www.google.com/recaptcha/api.js" async defer></script>
     <script>
       function onSubmit(token) {
         document.getElementById("demo-form").submit();
       }
     </script>
  </head>
  <body>
    <form id='demo-form' action="?" method="POST">
      <!-- Oops.... avoid the name="submit" below -->
      <button name="submit" class="g-recaptcha" data-sitekey="your_site_key" data-callback='onSubmit'>Submit</button>
      <br/>
    </form>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)


Tre*_*v14 8

当您的回调代码导致错误时,可能会发生这种情况。就我而言,我的回调只是引用了一些不存在的变量,我看到了同样的错误。对于如此简单的事情,非常奇怪的错误!

当我.意外地在变量名之后留下 a 时,我也看到了同样的错误。似乎这是一个超级通用的错误,意味着fix the code in your callback!.

  • 其他答案对我没有帮助,这个答案对我有帮助。结果我的回调函数中有一个愚蠢的错误(打字错误)。显然,Google reCAPTCHA 回调是基于承诺的,这掩盖了“真正的”问题,你只是没有得到像样的堆栈跟踪。在仔细阅读并重新阅读我的回调函数,并发现并修复错误/错误后,这条晦涩的消息“未捕获(承诺)空”消失了。 (2认同)

Kid*_*lly 7

我觉得我应该根据我的具体经验在这里添加一个答案。我相信最高答案,这将成为我答案的一部分。

我得到:Uncaught (in promise) null。当我在控制台中展开错误时,它是空的。

我从这个改变了我的代码:

function onSubmit(token) {
    if (grecaptcha.getResponse() !== "") {
        $('#request-form').submit();
    }
    grecaptcha.reset();
}
Run Code Online (Sandbox Code Playgroud)

对此:

function onSubmit(token) {
    return new Promise(function (resolve, reject) {

        if (grecaptcha.getResponse() !== "") {
            $('#request-form').submit();
        }
        grecaptcha.reset();

    });
}
Run Code Online (Sandbox Code Playgroud)

此更改的作用是允许您在控制台中接收特定的错误消息。然后,您可以继续解决您的特定问题。