无法在JQuery Ajax返回时刷新reCaptcha

Joh*_*ger 5 javascript ajax jquery recaptcha

我有一段时间试图让Ajax自动刷新JQuery AJAX回调.我有一个评论框,在reCaptcha验证后立即刷新消息,如果reCaptcha可以自动刷新,以防有人想在之后立即添加另一个评论,那将是很好的.这是我的返回功能:

 $.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()");   
        }
Run Code Online (Sandbox Code Playgroud)

我用的时候:

$.post("http://www.google.com/recaptcha/api", "Recaptcha:reload()"); 
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

XMLHttpRequest cannot load http://www.google.com/recaptcha/api. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin. 
Run Code Online (Sandbox Code Playgroud)

很公平,所以我试着改变这一行:

$('#recaptcha_reload_btn').trigger('click'); 
Run Code Online (Sandbox Code Playgroud)

但仍然没有发生任何事情.有谁知道发生了什么?

小智 6

在我的HTML中我有:

<div class="g-recaptcha" id='recaptcha' data-sitekey="xxxxxxxxxx"></div>
Run Code Online (Sandbox Code Playgroud)

我用 grecaptcha.reset();

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


mar*_*rue -1

您尚未将单击处理程序绑定到您的元素。来自 jQuery 文档:

.trigger( 事件类型 [, extraParameters] )

描述:执行附加到给定事件类型的匹配元素的所有处理程序和行为。

如果在调用触发器之前尚未将处理程序附加到元素,则没有要执行的单击处理程序。

编辑:

当您编写时,$('#recaptcha_reload_btn').bind('click');您实际上设置了一个单击处理程序,但它不执行任何操作。您必须告诉点击处理程序要做什么:

$('#recaptcha_reload_btn').bind('click',function() {
    alert('you just clicked me');
};
Run Code Online (Sandbox Code Playgroud)

如果您没有设置回调处理程序,则只会注册该事件,但不会触发任何操作。将其与此问题的解决方案结合使用 jQuery Reload an iframe,您应该让您的 recapcha 执行您想要的操作:

$('#recaptcha_reload_btn').bind('click',function() {
    var iframe = document.getElementById('#recaptcha'); //use your own selector here!
    iframe.src = iframe.src;
};
Run Code Online (Sandbox Code Playgroud)