0

我想在注册表单中使用 re-captcha 3,我想在单击按钮时检查 re-captcha。

我用这篇文章

我在注册按钮中有这个代码

$("#Register").click(function() {
   var $this = $(this);
   if ($(".ThemeContainer input[type='radio']:checked").length == 0 || !$("form").valid()) {
     return false;
   }
   if (!$("#@Html.IdFor(m => m.Confirm)").is(":checked")) {
     return false;
   }
  if ($("form").valid()) {
   $.ajax({
    url: '@Url.Action("Register", "Account")',
    data: $("form").serialize() ,
    type: 'POST',
    success: function(data) {
    if (data.IsSuccess) {
    window.location.href = data.Url;
    } else {
    $("#FinalMessage").html(data.Message);
    }
   } ,
     error: function() {
     }
   });
        }
         return false;
    });

如何在grecaptcha.execute其中使用?

public static IHtmlString ReCaptchaJS(this HtmlHelper helper, string useCase = "homepage")  
{  
    string reCaptchaSiteKey = GoogleReCaptchaVariables.ReCaptchaSiteKey;  
    string reCaptchaApiScript = "<script src='https://www.google.com/recaptcha/api.js?render=" + reCaptchaSiteKey + "'></script>;";  
    string reCaptchaTokenResponseScript = "<script>$('form').submit(function(e) { e.preventDefault(); grecaptcha.ready(function() { grecaptcha.execute('" + reCaptchaSiteKey + "', {action: '" + useCase + "'}).then(function(token) { $('#" + GoogleReCaptchaVariables.InputName + "').val(token); $('form').unbind('submit').submit(); }); }); }); </script>;";  
    return MvcHtmlString.Create($"{reCaptchaApiScript}{reCaptchaTokenResponseScript}");  
}
4

0 回答 0