"不是机器人"验证码无<形式GT;但AJAX代替 [英] "Not a robot" recaptcha without a <form> but AJAX instead

查看:146
本文介绍了"不是机器人"验证码无<形式GT;但AJAX代替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的传统方式我不是机器人Recpatcha 似乎是一个<形式GT; 上的客户端:

The traditional way of using "I am not a robot" Recpatcha seems to be with a <form> on client-side:

<form action="post.php" method="POST">
    <div class="g-recaptcha" data-sitekey="6Lc_0f4SAAAAAF9ZA_d7Dxi9qRbPMMNW-tLSvhe6"></div>
    <button type="submit">Sign in</button>
</form>

<script src='https://www.google.com/recaptcha/api.js'></script>

然后一些 G-验证码响应将被发送到服务器。

Then some g-recaptcha-response will be sent to server.

不过,在我的code我不使用&LT;形式GT; 但AJAX调用来代替:

However, in my code I don't use a <form> but an AJAX call instead:

$('#btn-post').click(function(e) { 
  $.ajax({
    type: "POST",
    url: "post.php",
    data: {
      action: 'post',
      text: $("#text").val(),
      ajaxMode: "true"
    },
    success: function(data) { },
    error: function(data) { } 
  }); } });

**如何获得 G-验证码响应这个解决方案的答案?

**How to get the g-recaptcha-response answer with this solution?

推荐答案

您使用的一种形式,中断形式提交。设置窗体按正常的:

You use a form, interrupt the submissions of the form. Set up a form as per normal:

<form action="post.php" method="POST" id="my-form">
    <div class="g-recaptcha" data-sitekey="6Lc_0f4SAAAAAF9ZA_d7Dxi9qRbPMMNW-tLSvhe6"></div>
    <input type="text" id="text">
    <button type="submit">Sign in</button>
</form>

<script src='https://www.google.com/recaptcha/api.js'></script>

然后你使用jQuery中断提交的形式和 序列化,让您通过Ajax的传递数据:

And then you use jQuery to interrupt the submission of the form and serialize it, allowing you to pass the data through Ajax:

$('#my-form').submit(function(e) {
    e.preventDefault();
    $this = $(this);
    $.ajax({
        type: "POST",
        url: "post.php",
        data: $this.serialize()
    }).done(function(data) {
    }).fail(function( jqXHR, textStatus ) {
        alert( "Request failed: " + textStatus );
    });
});

正如你会注意到我使用了 .done .fail 而不是成功错误,这是<一href="http://stackoverflow.com/questions/8840257/jquery-ajax-handling-continue-responses-success-vs-done">$p$pferred处理响应的方式。

As you will have noticed I've used .done and .fail instead of success and error, this is the preferred way of handling the response.

这篇关于&QUOT;不是机器人&QUOT;验证码无&LT;形式GT;但AJAX代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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