请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

javascript - Google reCaptcha 响应 "Uncaught (in promise) null"

[复制链接]
菜鸟教程小白 发表于 2022-8-14 07:37:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

安慰:

enter image description here

我的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-推荐答案


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/

    回复

    使用道具 举报

    懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关注0

    粉丝2

    帖子830918

    发布主题
    阅读排行 更多
    广告位

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap