问题
I am running into an issue with implementing the reCaptcha control on a contact form.
The following error is thrown on load: Uncaught ReferenceError: Recaptcha is not defined
Here is a partial snippet of the code I am using:
<form role="form" id="ContactMessageForm">
<div class="form-group">
<div id="CaptchaContainer"></div>
</div>
<button type="button" class="btn btn-default" id="ContactMessageSendButton">Send</button>
</form>
<script type="text/javascript" src="/scripts/mail.min.js" defer="defer"></script>
<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
<script type="text/javascript">
$(document).ready(function () {
/* Initialize reCaptcha Control */
Recaptcha.destroy();
Recaptcha.create('6Lc4V_ASAAAAAMnnwUcaTewH1mlOdylMgAyxb_m6', 'CaptchaContainer', {
theme: 'clean',
callback: Recaptcha.focus_response_field
});
});
</script>
There is most likely a typo in my code, but I am just not seeing it.
You can see the actual error get fired at: http://eat-sleep-code.com/#!/contact
回答1:
I have had the same problem and i fixed it with following
<script type="text/javascript">
$.getScript( "http://www.google.com/recaptcha/api/js/recaptcha_ajax.js", function() {
Recaptcha.create("<PUBLIC_KEY>", "recaptchadiv", {theme: "clean"});
});
</script>
回答2:
Using the feedback from @progysm I moved the call to recaptcha_ajax.js so that it was loaded before the renderContent() function was called. This allowed the Captcha image to be displayed.
Unfortunately, I ended up having to abandon the reCaptcha control for this application as the validation methods were blocked due to cross site scripting methods.
回答3:
Please confirm if you have given in the right public key and not the private key as following
Recaptcha.create('<public_key>', 'CaptchaContainer', {
theme: 'clean',
callback: Recaptcha.focus_response_field
});
来源:https://stackoverflow.com/questions/22540612/uncaught-referenceerror-recaptcha-is-not-defined