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回调有关(在你的情况下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中的第一个答案,所以请耐心等待,如果我忘记或错过了什么,请告诉我:)
Eli*_*ant 17
@ChristianŽagarskas在评论中指出:
原来,当网站未在Google Recaptcha / admin域区域中“注册”时,也会发生这种情况。
从开发密钥切换到生产密钥时,我犯了这个错误。生产密钥没有本地主机的任何条目。
我将API响应配置为位于代理重定向之后。因此,验证是在未在Google管理控制台中配置的localhost环境下进行的,导致此一般错误。
如果您的验证位于代理重定向的后面,请解决此错误的步骤:
Run Code Online (Sandbox Code Playgroud)localhost 127.0.0.1
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)
当您的回调代码导致错误时,可能会发生这种情况。就我而言,我的回调只是引用了一些不存在的变量,我看到了同样的错误。对于如此简单的事情,非常奇怪的错误!
当我.意外地在变量名之后留下 a 时,我也看到了同样的错误。似乎这是一个超级通用的错误,意味着fix the code in your callback!.
我觉得我应该根据我的具体经验在这里添加一个答案。我相信最高答案,这将成为我答案的一部分。
我得到: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)
此更改的作用是允许您在控制台中接收特定的错误消息。然后,您可以继续解决您的特定问题。
| 归档时间: |
|
| 查看次数: |
20786 次 |
| 最近记录: |