首页 > 解决方案 > 在recaptcha中成功后如何重定向

问题描述

在验证码尝试成功后,我正在尝试将该人重定向到某个位置。我使用了这段代码,但这不起作用。

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
 <script type="text/javascript"> 
 if (grecaptcha.getResponse() == "success")
  {
     function Redirect()
      {
         window.location.href = "example.com";
         }

           setTimeout(Redirect,1000);
            }
             else alert("You can't proceed!");
              </script>

  <div data-type="image" class="g-recaptcha" data-sitekey="my-sitekey"></div> 

我认为它不起作用,因为我不了解https://www.google.com/recaptcha/api/siteverify api。你能举一个代码的例子吗?我想在 recaptcha 成功后在 example.com 之后重定向,否则向他发送“您无法继续”

标签: javascripthtml

解决方案


The problem is, when the recaptcha returns 'success' you are creating a function called Redirect but you are not calling it, so you have to call the function after create it:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script type="text/javascript">
    if (grecaptcha.getResponse() == "success") {
        function Redirect() {
            window.location.href = "https://www.youtube.com/";
        }

        Redirect()

        setTimeout(Redirect, 1000);
    } else alert("You can't proceed!");
</script>

<div data-type="image" class="g-recaptcha" data-sitekey="my-sitekey"></div>

Or you can delete the function and redirect directly Make sure you are adding the full url:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script type="text/javascript">
    if (grecaptcha.getResponse() == "success") {
        window.location.href = "https://www.youtube.com/";

        setTimeout(Redirect, 1000);
    } else alert("You can't proceed!");
</script>

<div data-type="image" class="g-recaptcha" data-sitekey="my-sitekey"></div>

推荐阅读