【问题标题】:Google reCAPTCHA not working in .NET WebFormsGoogle reCAPTCHA 在 .NET WebForms 中不起作用
【发布时间】:2020-04-17 17:37:52
【问题描述】:

试图在我的一个网页中包含一个 google reCAPTCHA,但我收到了错误(在 chromes 开发人员工具中):

未捕获(承诺中)错误:缺少必需的参数:sitekey

在新的 dz (recaptcha__en.js:272)

在新的 GB (recaptcha__en.js:511)

在 Object.Rw [作为渲染] (recaptcha__en.js:509)

在renderRecaptcha (recaptchatest:93)

这是我的aspx设计代码;

<div id="ReCaptchContainer"></div> 
            <asp:Label ID="lblMEssage" runat="server" clientidmode="static"></asp:Label>
        <br />  
            <asp:Button type="button" ID="btnSubmit" runat="server" Text="Submit" />

这是我的 JS 和 jQuery:

<script src="https://www.google.com/recaptcha/api.js?onload=renderRecaptcha&render=explicit" async defer></script>

    <script  src="https://code.jquery.com/jquery-3.2.1.min.js"></script>  

    <script type="text/javascript">  
        var your_site_key = '<%= ConfigurationManager.AppSettings["6LfaeeoUAAAAAAhiEswkFeJ278PnF3-XXXXXXXXX"]%>';  
        var renderRecaptcha = function () {  
            grecaptcha.render('ReCaptchContainer', {  
                'sitekey': your_site_key,  
                'callback': reCaptchaCallback,  
                theme: 'light', //light or dark    
                type: 'image',// image or audio    
                size: 'normal'//normal or compact    
            });  
        };  

        var reCaptchaCallback = function (response) {  
            if (response !== '') {  
                jQuery('#lblMessage').css('color', 'green').html('Success');  
            }  
        };  

        jQuery('button[type="button"]').click(function(e) {  
            var message = 'Please checck the checkbox';  
            if (typeof (grecaptcha) != 'undefined') {  
                var response = grecaptcha.getResponse();  
                (response.length === 0) ? (message = 'Captcha verification failed') : (message = 'Success!');  
            }  
            jQuery('#lblMessage').html(message);  
            jQuery('#lblMessage').css('color', (message.toLowerCase() == 'success!') ? "green" : "red");  
        });  

    </script>

请注意,我已更改 API 密钥的最后 9 位数字。

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: javascript jquery .net recaptcha


    【解决方案1】:

    我很确定您用于应用程序设置的密钥是错误的:

    var your_site_key = '<%= ConfigurationManager.AppSettings["6LfaeeoUAAAAAAhiEswkFeJ278PnF3-XXXXXXXXX"]%>';
    

    也许它应该类似于ConfigurationManager.AppSettings["your-key"],返回值应该是"6LfaeeoUAAAAAAhiEswkFeJ278PnF3-XXXXXXXXX"

    更新:ConfigurationManager.AppSettings["your-key"] 用于从web.config &lt;appSettings&gt; 部分加载配置。如果您不使用应用程序设置,那么您只需直接添加您的密钥:

    var your_site_key = "6LfaeeoUAAAAAAhiEswkFeJ278PnF3-XXXXXXXXX";
    

    【讨论】:

    • 我试过这个 - var your_site_key = AppSettings["6LfaeeoUAAAAAAhiEswkFeJ278PnF3-XXXXXXXXX"];并得到错误 - Uncaught ReferenceError: AppSettings is not defined at recaptchatest:91
    • 这是一个很大的误解。你知道ConfigurationManager.AppSettings 是干什么用的吗?您的项目中有web.config 文件和&lt;appSettings&gt; 吗?
    猜你喜欢
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 2023-03-19
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多