gpt4 book ai didi

requirejs - 验证码 + RequireJS

转载 作者:行者123 更新时间:2023-12-02 23:47:23 42 4
gpt4 key购买 nike

如何使用 requirejs 导入 recaptcha。我已经尝试了几件事,但没有任何效果。

我需要这样做,以便能够在加载后使用 reCaptcha 的渲染方法自行渲染它。

require.config({
paths: {
'recaptcha': 'http://www.google.com/recaptcha/api'
}
});

require( ['recaptcha'], function( recaptcha ) {
// do something with recaptcha
// recaptcha.render /// at this point recaptcha is undefined
console.log(recaptcha);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>

最佳答案

是的,我有一个解决方案给你。因此,最终发生的情况是,在从 google api 加载所需内容之前,recaptcha 无法呈现。

因此您需要执行以下操作(也不要在路径中使用 http/https):

require.config({
paths: {
'recaptcha': '//www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit'
}
});

现在,从 google API 下载必要的库后,将允许执行回调。不幸的是,这个回调需要是全局的。

JS

var requireConfig = {
paths: {
'recaptcha': '//www.google.com/recaptcha/api.js? onload=onloadCallback&render=explicit'
}
};

function render(id) {
console.log('[info] - render');
recaptchaClientId = grecaptcha.render(id, {
'sitekey': '6LdntQgUAAAAANdffhZl0tIHw0fqT3MwNOlAI-xY',
'theme': 'light'
});
};
window.renderRecaptcha = render;

var onloadCallback = function() {
console.log('[info] - onLoadCallback');
if (!document.getElementById('g-recaptcha')) {
return;
}
window.renderRecaptcha('g-recaptcha');
};

requirejs.config(requireConfig);

require(['recaptcha'], function(recaptcha) {

});

HTML

<body>
<form action="?" method="POST">
<div id="g-recaptcha"></div>
<br/>
<input type="submit" value="Submit">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.js"> </script>


</body>

我希望这对你有用!

示例链接:https://jsbin.com/kowepo/edit?html,js,console,output

关于requirejs - 验证码 + RequireJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854128/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com