通过javascript获取reCaptcha值? [英] Get reCaptcha value via javascript?

查看:296
本文介绍了通过javascript获取reCaptcha值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将reCaptcha添加到我的网站,并且当我以某种形式正常使用它时,一切都很好用,但是现在当我尝试通过JavaScript或jQuery读取验证码值时遇到了一个问题.我有此HTML代码可在页面上加载reCaptcha.

I've added reCaptcha to my site, and when I use it normally in a form everything works great, but now I faced a problem when I tried to read the captcha value via JavaScript or jQuery. I have this HTML code that loads reCaptcha on my page.

<div align="center">
    <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=mySecretkey321"></script>
    <noscript>
        <iframe src="http://www.google.com/recaptcha/api/noscript?k=mySecretkey321" height="300" width="500" frameborder="0"></iframe><br>
        <textarea id="rcf" name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
        <input type="hidden" id="rrf" name="recaptcha_response_field" value="manual_challenge">
    </noscript>
    <input type="button" value="Ok!" id="ok" class="btn btn-danger btn-lg"/>
</div>

当我填写验证码字段并单击一个按钮时,我想加载 recaptcha_response_field recaptcha_challenge_field 的值,以便我可以通过$ .get()提交它们;所以我想出了这段代码:

When I fill in the captcha field and click on a button I want to load the values of recaptcha_response_field and recaptcha_challenge_field so I can submit them via $.get(); so I've come up with this code:

$("#ok").click(function() {
    var res,res2;
    var rcf = document.getElementsByName('recaptcha_challenge_field').value;
    var rrf = document.getElementsByName('recaptcha_response_field').value;
    var url = "go.php?rcf="+ rcf +"&rrf=" + rrf;
    alert(url);
    $.get( url, function( data ) {
      var n = data.indexOf("#");
      res = data.slice(0,n);
      res2 = data.slice(n+1);
    });
});

当我单击按钮时,将显示一个具有以下值的警报框:

When I click the button I get an alert box with this value:

go.php?rcf=undefined&rrf=undefined

基本上我看不懂这些字段,我认为我读错了字段.文件go.php验证了验证码,我只需要将这两个验证码字段传递给它,这样它就可以完成检查验证码的所有后台工作.

Basically I can't read those fields and I think that I'm reading the wrong ones. File go.php verifies captcha I just need to pass those two captcha fields to it so it can do all the background work of checking the captcha.

我真的不知道如何实现这一目标,我想有一个简单的解决方案,但是我在任何地方都找不到它,所以我想向社区询问.

I really don't know how to achieve this and I guess there is a simple solution, but I couldn't find it anywhere so I wanted to ask the community.

最诚挚的问候!

推荐答案

document.getElementsByName函数返回元素的集合,顾名思义-它是元素(带有s-复数).该集合没有定义value属性,这就是为什么尝试访问它会返回undefined的原因.

The document.getElementsByName function returns a collection of elements, as the name sort of implies - it's elements (with an s - plural). That collection doesn't have a value property defined on it, which is why trying to access it returns undefined.

如果您确定只有一个带有这些名称的元素(不多也不少),则可以访问集合中的第一个索引(这是您想要的元素)并获取其值:

If you're sure that there's only ever going to be exactly one (no more and no fewer) elements with those names, you can access the first index in the collection (which is the element you want) and get its value:

var rcf = document.getElementsByName('recaptcha_challenge_field')[0].value;
var rrf = document.getElementsByName('recaptcha_response_field')[0].value;

或者,您可以使用jQuery(因为您已经加载了jQuery,以便将其用于AJAX调用)来获取值:

Alternatively, you could use jQuery (since you've already loaded it to use it for the AJAX call) to get the values:

var rcf = $('[name="recaptcha_challenge_field"]').val();
var rrf = $('[name="recaptcha_response_field"]').val();

这篇关于通过javascript获取reCaptcha值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆