Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google reCaptcha doesn't get loaded when Bootstrap call the remote modal

I have defined in my first page something like this :

<span class="btn btn-default"> <a data-toggle="modal"
        id="fillTheFormHiddenInput" data-target="#login-modal" href="login-i">sign in</a>
</span>

and at the end of the first page :

<div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</div>

and this is my second page: (.../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>

actually I'm calling a remote modal with this way. but when I click the login button, the reCaptcha doesn't get loaded and this will show out:

enter image description here

Reload the page to get source for: http://api.recaptcha.net/challenge...

I also noticed that the status code is 302 when the script is getting loaded :

enter image description here

what is the problem ??? (for you to know if I load the page login-i without modal the reCaptcha DOES show out)


here is the simplified version of the project, you can take a look at it ...

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

like image 623
Mehdi Avatar asked Apr 19 '14 20:04

Mehdi


People also ask

Why reCAPTCHA is not showing sometimes?

One of the most common reasons why this error occurs is that of an outdated Chrome version. reCAPTCHA will actively look at the browser version before allowing you access. This is applicable to all browser versions, not just Chrome. In this case, the solution is to update Google Chrome to the latest version.

Can I run reCAPTCHA V2 and v3 on the same page?

Yes, you can use both reCAPTCHA (non-Enterprise version) and reCAPTCHA Enterprise. Typically the third party solution asks for your public key and either your secret key or your API key.

How do I trigger Google v3 reCAPTCHA?

The easiest method for using reCAPTCHA v3 on your page is to include the necessary JavaScript resource and add a few attributes to your html button. Load the JavaScript API. Add a callback function to handle the token. Add attributes to your html button.


1 Answers

The problem is that recaptcha code run with document.write() method. When you call ajax, you can't use that method because of ajax. For example, data-remote in modal use ajax internally.

If you run the code in chrome, you can see the warning below.

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.

Google provide other way of recaptcha for solving the problem. You can add the script in your index.jsp first.

<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>

And then, replace the code in login.jsp.

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

to...

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

That's all. ReCaptcha will show on the modal.

FYI, you need to verify the form. Verifying the User's Answer Without Plugins will be helpful.

like image 167
Edward Avatar answered Oct 31 '22 13:10

Edward