5

I'm having a devil of a time trying to get Ajax to automatically refresh on a JQuery AJAX callback. I have a comment box with the messages being refreshed posted immediately upon validation of reCaptcha and it would be nice if the reCaptcha could refresh automatically in case someone wants to add another comment immediately afterward. Here's my return function:

 $.post(url, formData, function(data) {
        if (returnString.match(/^Error:/)) {
          $("#interactionResults").html(data).show().fadeOut(6000);
        }
        else if (postNumber == 0) {
          $('#newCommentDisplay').html(returnString).show();
          $.post("http://www.google.com/recaptcha/api", "Recaptcha:reload()");   
        }

When I use:

$.post("http://www.google.com/recaptcha/api", "Recaptcha:reload()"); 

I get an error:

XMLHttpRequest cannot load http://www.google.com/recaptcha/api. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin. 

Fair enough, so I try to change that line with this one:

$('#recaptcha_reload_btn').trigger('click'); 

and still nothing is happening. Does anyone know what's going on?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
John Bowlinger
  • 1,561
  • 4
  • 12
  • 14
  • ah, by "nothing is happening" do literally mean nothing, or do you still talk of a Google error? – marue Apr 04 '12 at 08:09
  • Yeah, I literally mean nothing. Sorry I didn't make that clearer. – John Bowlinger Apr 04 '12 at 13:11
  • Then you need to show your click-handler. Has to be some problem there. – marue Apr 04 '12 at 14:02
  • @marue It's in an Iframe which is what I think may be tripping me up. I think I may need to send a JSON object back to Google. Here's the iframe:
    – John Bowlinger Apr 05 '12 at 04:18
  • Hm, it's not so easy to catch events from an iframe, i'm not even sure if it is possible. If you have outside that iframe, you could use the answer in this post http://stackoverflow.com/questions/4249809/reload-an-iframe-with-jquery – marue Apr 05 '12 at 08:18

6 Answers6

6

in my html I have :

<div class="g-recaptcha" id='recaptcha' data-sitekey="xxxxxxxxxx"></div>

I use grecaptcha.reset();

example: if (typeof $('#recaptcha') != "undefined") { grecaptcha.reset(); }

Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
3

Use

jQuery("#recaptcha_reload").click(); 

"#recaptcha_reload", the image itself, is the trigger. #recaptcha_reload_btn will NOT work, because the a-tag is NOT the trigger.

Proximo
  • 41
  • 3
  • +1 because it works no matter which library is used to display the captcha. However `Recaptcha.reload();` is better if you happen to be using the javascript library. – Steen Schütt Jan 27 '14 at 15:46
2
Recaptcha.reload();

( Recaptcha is already loaded )

Sammy
  • 4,538
  • 8
  • 33
  • 52
0

if you are using new recaptcha 2.0 use this: for code behind:

ScriptManager.RegisterStartupScript(this, this.GetType(), "CaptchaReload", "$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});", true);

for simple javascript

<script>$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});</script>
Sara Khan
  • 325
  • 1
  • 4
  • 9
-1

You have not bound a click handler to your element. From the jQuery docs:

.trigger( eventType [, extraParameters] )

Description: Execute all handlers and behaviors attached to the matched elements for the given event type.

If you have not attached a handler to your element before calling trigger, there is no click handler to execute.

edit:

When you write $('#recaptcha_reload_btn').bind('click'); you actually set up a click handler, but one that does nothing. You have to tell that clickhandler what to do:

$('#recaptcha_reload_btn').bind('click',function() {
    alert('you just clicked me');
};

If you do not set up a callback handler, the event is just registered but does not trigger any action. Combine this with the solution from this question Reload an iframe with jQuery and you should have your recaptcha doing what you want:

$('#recaptcha_reload_btn').bind('click',function() {
    var iframe = document.getElementById('#recaptcha'); //use your own selector here!
    iframe.src = iframe.src;
};
Community
  • 1
  • 1
marue
  • 5,588
  • 7
  • 37
  • 65
-1
$( "#recaptcha_reload_btn" ).click();
Botz3000
  • 39,020
  • 8
  • 103
  • 127
napstyr maceda
  • 179
  • 3
  • 13