如何在 MVC3 中将 @ReCaptcha 与 Ajax 表单一起使用?

发布于 2024-11-03 09:59:26 字数 823 浏览 0 评论 0原文

我正在尝试使用 Microsoft.Web.Helpers 中的 ReCaptcha。如果我加载整个页面,它会正确呈现,但如果我使用 ajax 请求加载页面,它就会消失。

示例 (/home/index)

<div id="bla">
    @Ajax.ActionLink("reload with ajax", "index", new AjaxOptions() { UpdateTargetId = "bla" })

    @ReCaptcha.GetHtml(publicKey: "xxx")
</div>

如果我输入 /home/index 就会出现验证码。如果我点击按钮使用ajax重新加载,ReCaptcha就会消失...

recaptcha

  1. 页面首次加载
  2. reload with单击ajax,页面内容更改为/home/index,换句话说,整个页面异步重新加载,验证码消失了

有没有办法解决这个问题或MVC 3 的不错的验证码助手?

I am trying to use ReCaptcha from Microsoft.Web.Helpers. If I load the entire page it renders correctly, but if I load the page with an ajax request it disappears.

Example (/home/index)

<div id="bla">
    @Ajax.ActionLink("reload with ajax", "index", new AjaxOptions() { UpdateTargetId = "bla" })

    @ReCaptcha.GetHtml(publicKey: "xxx")
</div>

If I enter /home/index the captcha appears. If I click the button reload with ajax the ReCaptcha disappears...

recaptcha

  1. The page is loaded for the first time
  2. reload with ajax was clicked, the contents of the page change to /home/index, in other words, the entire page reloaded asynchronous and the captcha is gone

Is there a way to fix this or a decent captcha helper for MVC 3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽手叙旧 2024-11-10 09:59:26

我已将助手替换为 javascriptReCaptcha 脚本

<div id="captcha"></div>

<input type="submit" value="Send" />

<script type="text/javascript">
Recaptcha.destroy();
Recaptcha.create("publicKey", "captcha", {});
</script>

并且控制器仍然相同,

if (ReCaptcha.Validate("privateKey"))
{
}

因此当它部分加载视图时,它会执行此脚本并每次都正确渲染。

感谢您的帮助@Bala R

I've replaced the helper with javascript. ReCaptcha script

<div id="captcha"></div>

<input type="submit" value="Send" />

<script type="text/javascript">
Recaptcha.destroy();
Recaptcha.create("publicKey", "captcha", {});
</script>

And the Controller is still the same

if (ReCaptcha.Validate("privateKey"))
{
}

So when it loads the view partially it executes this scripts and render correctly every time.

Thanks for the help @Bala R

过潦 2024-11-10 09:59:26

我遇到了同样的问题,我发现最快的解决方案是使用上面建议的内容,并将这部分代码添加到 .net javascript api ( http://msdn.microsoft.com/en-us/library/bb311028(v =vs.100))。

使用此解决方案,后端验证始终有效。

这是我使用的代码:

<script type="text/javascript" language="javascript">
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
  function EndRequestHandler(sender, args) {
            if (Recaptcha != null) {
                     Recaptcha.destroy();
                     Recaptcha.create("public_key", "captcha", {});
                  }
              }
</script>

我希望这可以帮助某人......

I faced the same issue and the quickest solution i found is using what it is proposed above and add to it this part of code in the top of your page within the handler "EndRequestHandler" proposed by the .net javascript api ( http://msdn.microsoft.com/en-us/library/bb311028(v=vs.100)).

With this solution the backend validation always works.

Here is the code I've used :

<script type="text/javascript" language="javascript">
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
  function EndRequestHandler(sender, args) {
            if (Recaptcha != null) {
                     Recaptcha.destroy();
                     Recaptcha.create("public_key", "captcha", {});
                  }
              }
</script>

I hope this could help someone...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文