我使用的是reCaptcha v2,但在任何情况下都是在dev控制台响应Uncaught (in promise) null (并移动.reset()函数)。
控制台:

我的验证码:
<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>我的回调功能:
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();
}还有我的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');在网上搜索,问题可能是.reset()函数,但我不明白解决方案。
发布于 2018-09-21 02:16:25

我也犯了这个错误,我发现这与recaptcha回调有关(在您的例子中是data-callback="callback")。如果删除数据回调属性,就不会出现错误。
控制台错误Uncaught (in promise) null表示回调正在等待承诺。下面是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
};在这种情况下,需要将代码调整为如下所示:
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
}这是我第一次回答这个问题,所以请耐心点,如果我忘了或错过了什么,请告诉我:)
发布于 2019-05-23 08:26:55
结果发现,当一个站点没有在Google /admin域中“注册”时,它也会发生。
解决方案:在recaptcha管理区域中添加域:
本地主机127.0.0.1
当我从开发密钥切换到生产密钥时,我犯了这个错误。生产密钥没有本地主机的任何条目。
我将API响应配置为位于代理-重定向之后。因此,验证是在一个本地主机环境中工作的,这个本地主机环境没有在Google控制台中进行配置,这导致了这个通用错误。
这要归功于@ChristianŽagarskas,他在评论中指出了这一点。
发布于 2018-12-03 10:28:58
困扰我的另一个错误触发因素是表单中有一个按钮,其名称属性为“submit”。如果使用自动绑定reCaptcha文档中的示例代码,这会使它出错,因为'form.submit‘将引用按钮,而不是表单本身的submit()函数。哈!
<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>https://stackoverflow.com/questions/52390562
复制相似问题