Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Not a robot" recaptcha without a <form> but AJAX instead

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>

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


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) { } 
  }); } });

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

like image 936
Basj Avatar asked Jun 04 '15 14:06

Basj


People also ask

How does reCAPTCHA know I'm not a robot?

reCAPTCHA works by taking any of the scanned words that cannot be recognised and presenting them to a human alongside a known word for interpretation. By typing the known word correctly, you identify yourself as a human and the reCAPTCHA system gains some confidence that you have correctly digitised the second.

Why does Google keep asking me to verify I'm not a robot?

Your computer may be infected with malware that sends automated traffic to Google. Also some browser extensions and plugins can send automated traffic. If you are frequently seeing “I'm not a robot” message then check your computer for malicious programs and remove unnecessary browser extensions.

Why is I'm not a robot not working?

If the I'm not a robot reCAPTCHA constantly spins after selecting the I'm not a robot reCAPTCHA box, you may be experiencing network or browser related issues. Please try either reloading the page or accessing the same page with a different browser such as Google Chrome or Mozilla Firefox.


2 Answers

I just implemented it without using any form and submit mechanism, respectively. Thus, a pure AJAX solution.

HTML code:

<div id="recaptcha-service" class="g-recaptcha"
 data-callback="recaptchaCallback"
 data-sitekey=""></div>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=en"></script>

JavaScript code:

window.recaptchaCallback = undefined;

jQuery(document).ready(function($) {

  window.recaptchaCallback = function recaptchaCallback(response) {
    $.ajax({
      method: "POST",
      url: "http://example.com/service.ajax.php",
      data: { 'g-recaptcha-response': response },
    })
      .done(function(msg) {
        console.log(msg);
      })
      .fail(function(jqXHR, textStatus) {
        console.log(textStatus);
      });
  }

});

The point is using a callback (recaptchaCallback in this case).

You can find a more complete example at https://gist.github.com/martinburger/f9f37770c655c25d5f7b179278815084. That example uses Google's PHP implementation on the server side (https://github.com/google/recaptcha).

like image 86
Martin Avatar answered Oct 21 '22 12:10

Martin


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>

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 );
    });
});

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.

like image 22
Styphon Avatar answered Oct 21 '22 13:10

Styphon