当Bootstrap调用远程模式时,Google reCaptcha不会被加载

Meh*_*hdi 5 javascript java jquery recaptcha twitter-bootstrap

我在第一页中定义了这样的内容:

<span class="btn btn-default"> <a data-toggle="modal"
        id="fillTheFormHiddenInput" data-target="#login-modal" href="login-i">sign in</a>
</span>
Run Code Online (Sandbox Code Playgroud)

并在第一页的末尾:

<div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的第二页:(.../login-i)

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>
<div class="modal-dialog" style="width: 350px !important;">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"
                aria-hidden="true">&times;</button>
            <h4 class="modal-title">Login to Dashboard</h4>
        </div>

        <form:form role="form" method="post" commandName="userCredential">
            <div class="modal-body border-top-less">

                <form:errors cssClass="" path="*" element="div" />

                <form:input path="username" class="form-control"
                    placeholder="Your username" id="inputUsername1" />
                <br />

                <form:password path="password" class="form-control"
                    placeholder="Your Password" id="inputPassword1" />
                <div>
                <%
                    ReCaptcha c = ReCaptchaFactory.newReCaptcha("6LdoF_ISAAAAAH3dYUqRZvpCwPCyH4lfBxdLy_a3", "6LdoF_ISAAAAAGxauxkNaSjv3DTBRmEvawWaklo_", false);
                        out.print(c.createRecaptchaHtml(null, null));
                %>
                </div>

                <br />
                <div class="form-group">
                    <button type="submit" class="btn btn-default btn-block">sign
                        in</button>
                </div>

                <div class="form-group text-center">
                    <a href="${routePath}signup">Sign Up Now !!</a>
                </div>



            </div>
        </form:form>

    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

实际上我用这种方式调用远程模态.但是当我点击登录按钮时,reCaptcha没有被加载,这将显示出来:

在此输入图像描述

重新加载页面以获取源代码:http://api.recaptcha.net/challenge ...

我还注意到脚本加载时状态代码为302:

在此输入图像描述

问题是什么 ???(你知道我是否加载页面login-i没有模态,reCaptcha DOES显示出来)


这是项目的简化版,你可以看看它......

https://app.box.com/s/zduxdiafwzmsw2u6eqm7

Edw*_*ard 11

问题是recaptcha代码与document.write()方法一起运行.当你调用ajax时,你不能使用那个方法因为ajax.例如,data-remote在模态中在内部使用ajax.

如果您在chrome中运行代码,则可以在下面看到警告.

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
Run Code Online (Sandbox Code Playgroud)

谷歌提供了其他方法来重新解决问题.您可以在index.jsp第一个脚本中添加脚本.

<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后,替换中的代码login.jsp.

<%
  ReCaptcha c = ReCaptchaFactory.newReCaptcha("YOUR_API_KEY", "YOUR_TOKEN", false);
  out.print(c.createRecaptchaHtml(null, null));
%>
Run Code Online (Sandbox Code Playgroud)

至...

<div id="recaptcha"></div>
<script>
  Recaptcha.create("YOUR_API_KEY",   // update with your api key
    "recaptcha",
    {
      theme: "red",
      callback: Recaptcha.focus_response_field
    }
  );
</script>
Run Code Online (Sandbox Code Playgroud)

就这样.ReCaptcha将显示在模态上.

仅供参考,您需要验证表格.在没有插件的情况下验证用户的答案会很有帮助.