Google Invisible ReCaptcha并非隐身

时间:2017-05-02 08:52:41

标签: javascript jquery recaptcha captcha invisible

我只是在提交表单后尝试让Google Invisible ReCaptcha工作。 我的问题是,ReCaptcha不是看不见的,看起来像是“旧的”重新出现了。我不明白为什么。我的网站密钥是无形的recaptcha。请帮帮我。

首先我正在加载API:

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

我的表单如下:

<form method="post" id="contact-form-face" class="clearfix" onsubmit="return false">
<input type="text" required name="name" value="name" onFocus="if (this.value == 'name') this.value = '';" onBlur="if (this.value == '') this.value = 'name';" />
<button type="submit" id="sendButton" data-size="invisible" class="g-recaptcha contact_btn" onclick="onSubmitBtnClick()" value="send" >send</button>
</form>

JS:

window.onScriptLoad = function () {
// this callback will be called by recapcha/api.js once its loaded. If we used
// render=explicit as param in script src, then we can explicitly render reCaptcha at this point

// element to "render" invisible captcha in
var htmlEl = document.querySelector('.g-recaptcha');

// option to captcha
var captchaOptions = {
    sitekey: 'XXXXXXX',
    size: 'invisible',
    // tell reCaptcha which callback to notify when user is successfully verified.
    // if this value is string, then it must be name of function accessible via window['nameOfFunc'],
    // and passing string is equivalent to specifying data-callback='nameOfFunc', but it can be
    // reference to an actual function
    callback: window.onUserVerified
};

// Only for "invisible" type. if true, will read value from html-element's data-* attribute if its not passed via captchaOptions
var inheritFromDataAttr = true;

console.log("render now!");
// now render
recaptchaId = window.grecaptcha.render(htmlEl, captchaOptions, inheritFromDataAttr);
};

// this is assigned from "data-callback" or render()'s "options.callback"
window.onUserVerified = function (token) {
alert('User Is verified');
console.log('token=', token);

};

// click handler for form's submit button
function onSubmitBtnClick () {
var token = window.grecaptcha.getResponse(recaptchaId);

// if no token, mean user is not validated yet
if (!token) {
    // trigger validation
    window.grecaptcha.execute(recaptchaId);
    return;
}

var xhrData = {
    'g-recaptcha-response': token
    // more ajax body/data here
};

};

为了让事情变得更清楚:这个reCaptcha工作正常,回调加载,验证工作也正常....唯一的问题是,这个rec​​aptcha必须是invisibile,而不是:/

3 个答案:

答案 0 :(得分:1)

我认为您的问题是您明确调用了render()方法

recaptchaId = window.grecaptcha.render(htmlEl, captchaOptions, inheritFromDataAttr);

};

如果您希望它不可见,则必须包含此自定义DIV

    <div class="g-recaptcha"
      data-sitekey="your_site_key"
      data-callback="onSubmit"
      data-size="invisible">
    </div>

https://developers.google.com/recaptcha/docs/invisible

recaptcha脚本将查找div类元素并将其绑定在那里,然后在调用recaptcha验证时执行回调。

调用recaptcha验证使用grecaptcha.execute();

按照googledev页面中的示例,非常简单。

我希望帮助=)

答案 1 :(得分:1)

我认为您错过了“captchaOptions”中的badge参数 有3个可能的值:bottomright(默认值),bottomleft和 inline (可让您自定义recaptcha的css)。

答案 2 :(得分:0)

查看JavaScript API文档, https://developers.google.com/recaptcha/docs/invisible#js_api 您需要添加一个带有要呈现的id的div,当您使用JavaScript呈现验证码时,按钮元素上不需要类和数据属性。

您的HTML应该是:

<form method="post" id="contact-form-face" class="clearfix"   
onsubmit="return false">
    <input type="text" required name="name" value="name" onFocus="if 
    (this.value == 'name') this.value = '';" onBlur="if (this.value == 
    '') this.value = 'name';" />
    <div id="recaptcha"></div>
    <button type="submit" id="sendButton" class="contact_btn" 
    onclick="onSubmitBtnClick()"value="send">send</button>
</form>

你的JS应该更新为:

// element to "render" invisible captcha in
var htmlEl = 'recaptcha'