如何使用Ajax Jquery和Google Invisible Captcha提交表单

时间:2017-08-30 12:10:37

标签: javascript jquery html ajax recaptcha

我制作了一个Ajax表单,但在几天之前,我添加了Invisible Google Captcha。但现在,Google Captcha代码会重定向用户以形成action网址。

这是我的代码之前添加Captcha:



$("form.formajax").submit(function(e) {

  e.preventDefault();

  var data = $(this).serialize();
  var url = $(this).attr("action");
  var form = $(this); // Add this line
  
  $.post(url, data, function(data) {
  $(form).children(".signupresult").html(data.signupresult);

  });
  return false;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<form class="formajax" method="post" action="admin/login/logincheck.php">

  <input type="text" placeholder="username">
  <input type="submit" />

</form>
&#13;
&#13;
&#13;

以下是添加Captcha

后的代码

&#13;
&#13;
function onSubmit(token) {
  document.getElementById("i-recaptcha").submit();
}

$("form.formajax").submit(function(e) {

  e.preventDefault();
  var data = $(this).serialize();
  var url = $(this).attr("action");
  var form = $(this); // Add this line

  $.post(url, data, function(data) {
    $(form).children(".signupresult").html(data.signupresult);
  });
  return false;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

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

<form id='i-recaptcha' class="formajax" method="post" action="admin/login/logincheck.php">
  <input type="text" placeholder="username">
  <input type="submit" class="g-recaptcha" data-sitekey="6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk" data-callback="onSubmit" />

</form>
&#13;
&#13;
&#13;

可能是onSubmit函数导致问题。我怎么能把这个表格变成ajax?

0 个答案:

没有答案