简体   繁体   English

在函数调用中加载Javascript文件

[英]Load Javascript file on function call

I'm using Google Captcha when some number of login attempts failed, my website is based on Ajax and Java Script so I need to load Google Captcha Java Script file when a function called! 当某些登录尝试失败时,我正在使用Google Captcha ,我的网站基于Ajax and Java Script因此当调用函数时,我需要加载Google Captcha Java Script文件!

I'm getting captcha code through making a request to my API, http://www.myserver.com/api/?request=captcha so this will return 我通过向我的API http://www.myserver.com/api/?request=captcha发出请求来获取验证码,因此这将返回

<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LeV3rdSAAAAABp2Q3bjyjCR9E6vvZ06oM6-6yj"></script>

I use $POST to make the request and .HTML to show captcha on my site 我使用$POST发出请求,并使用.HTML在我的网站上显示验证码

$.post("api/index.php", {request: "captcha"}, function(data) {

    $('#customContent').html(data);
});

but this doesn't show up captcha on the HTML element! 但这不会在HTML元素上显示验证码! Where am I doing wrong? 我在哪里做错了?

Go to this Google page and follow the instructions for dynamically inserting Google captcha into a page. 转到此Google页面,然后按照说明将Google验证码动态插入页面中。 You must do it differently than you are. 您必须做的与您不同。 If you're going to fetch the code with ajax, then scroll down the above page until the section labeled AJAX API and follow their code example. 如果要使用ajax提取代码,请向下滚动上一页,直到标记为AJAX API的部分并遵循其代码示例。

You should read their directions exactly, but basically you put this in your page all the time: 您应该准确地阅读他们的指示,但基本上,您始终将其放在页面中:

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

You create a container div for where you want the captcha to go and give it an id and then you call this with your key and the id when you want to insert the captcha: 您在想要验证码的位置创建一个容器div并为其指定一个ID,然后在要插入验证码时使用密钥和ID进行调用:

Recaptcha.create("your_public_key",
    "element_id",
    {
      theme: "red",
      callback: Recaptcha.focus_response_field
    }
  );

FYI, I've never done this myself. 仅供参考,我从来没有自己做过。 I just Googled and read instructions. 我只是Google搜索并阅读说明。

Finally I found an answer myself and thanks for your answers! 最后,我自己找到了答案,感谢您的回答!

function showRecaptcha() {
        Recaptcha.create("your public key", 'captchadiv', {
        tabindex: 1,
        callback: Recaptcha.focus_response_field
        });
}  

and you don't need to load Google Captcha Java Script at the begging just use getScript function to load Google Captcha script when maximum failed logging attempts occurred and use .done responce status to call showRecaptcha() function. 并且您无需在开始时load Google Captcha Java脚本,只需在发生最大次数的失败日志记录尝试时使用getScript函数加载Google Captcha脚本,并使用.done响应状态调用showRecaptcha()函数。 good luck! 祝好运!

Don't use jQuery to put script tag that uses document.write, instead put it with native javascript, like: 不要使用jQuery放置使用document.write的脚本标记,而应使用本机javascript放置它,例如:

$.post("api/index.php", {request: "captcha"}, function(data) {

    $('#customContent')[0].appendChild(data);
});

I hope it helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM