我正在使用 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>
我的回调函数:
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' : 'OST',
'data' : {
'response' : response
},
'success' : function(data) {
alert('Data: '+data);
},
'error' : function(request,error)
{
alert("Request: "+JSON.stringify(request));
}
});
grecaptcha.reset();
}
和我的验证-recaptcha.php :
<?php
//debug
$fp = fopen('debug.txt', 'a');
fwrite($fp, print_r($_POST, TRUE));
fclose($fp);
//enddebug
if (empty($_POST['recaptcha'])) {
exit('lease 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' => 'OST',
'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() 的问题功能,但我不明白解决方案。
Best Answer-推荐答案 strong>
Turns out it also occurs when a site is not "registered" in the Google recaptcha/admin Domains area.
解决方案:在recaptcha管理区域添加域: 登录您的注册验证码的 Google 帐户 输入谷歌“google recpatcha 管理控制台”转到您的(生产) key 设置在“域”中,添加这两个条目:
localhost
127.0.0.1
保存并测试您的验证码。
当我从开发 key 切换到生产 key 时,我犯了这个错误。生产 key 没有任何 localhost 条目。
我将 API 响应配置为位于代理重定向之后。因此,验证是在未在 Google 管理控制台中配置的 localhost 环境中进行的,这会导致此一般错误。
感谢@Christian Žagarskas,他在评论中指出了这一点。
关于javascript - Google reCaptcha 响应 "Uncaught (in promise) null",我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/52390562/
|