javascript - "Not a robot" recaptcha without a <form> but AJAX instead -
the traditional way of using "i not robot" recpatcha seems <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 g-recaptcha-response
sent server.
however, in code don't use <form>
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 g-recaptcha-response
answer solution?
you use form, interrupt submissions of form. set form 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 use jquery interrupt submission of form , serialize it, allowing pass 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 have noticed i've used .done
, .fail
instead of success
, error
, preferred way of handling response.
Comments
Post a Comment