Javascript 在主干视图中呈现reCAPTCHA v2.0小部件
这是我的问题,我有一个index.html页面,可显式加载reCAPTCHA脚本:Javascript 在主干视图中呈现reCAPTCHA v2.0小部件,javascript,backbone.js,recaptcha,Javascript,Backbone.js,Recaptcha,这是我的问题,我有一个index.html页面,可显式加载reCAPTCHA脚本: <script src="https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit" async defer></script> 此时,我甚至无法触发回调函数(loadCaptcha),我怀疑问题在于加载顺序、加载索引页以及“onload=loadCaptcha”事件发生在主干视图初始化
<script src="https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit" async defer></script>
此时,我甚至无法触发回调函数(loadCaptcha),我怀疑问题在于加载顺序、加载索引页以及“onload=loadCaptcha”事件发生在主干视图初始化之前。我尝试从脚本标记中删除“async”属性,但没有成功。你知道我该怎么做吗 我会将您的captch脚本放在主干视图中。然后将其放入主视图的渲染中。这样,它是模块化的,您有一个主视图和一个子视图,其中包含在呈现主视图时加载的验证码。我通过直接从呈现它的视图中拉入recaptcha脚本,找到了解决方案。希望这对将来的人有所帮助
loadCaptcha: function() {
var self = this;
var getRecaptchaResponse = function(response) {
self.captchaResponse = response;
};
var renderCaptcha = function() {
self.captchaWidgetId = grecaptcha.render('recaptcha-container-' + self.model.get('Id'), {
sitekey : service.settings.recaptchaSiteKey,
callback: getRecaptchaResponse
});
};
window.renderCaptcha = renderCaptcha;
$.getScript('https://www.google.com/recaptcha/api.js?onload=renderCaptcha&render=explicit', function() {});
},
'use strict';
var Backbone = require('Backbone');
var Validation = require('backbone-validation');
Backbone.Validation = Validation;
Backbone.$ = $;
module.exports = BaseView.extend({
events: {},
formView: null,
initialize: function (options) {
var loadCaptcha = function() {
window.alert('captcha is ready');
};
},
render: function () {
// renders view using my form template
}
});
loadCaptcha: function() {
var self = this;
var getRecaptchaResponse = function(response) {
self.captchaResponse = response;
};
var renderCaptcha = function() {
self.captchaWidgetId = grecaptcha.render('recaptcha-container-' + self.model.get('Id'), {
sitekey : service.settings.recaptchaSiteKey,
callback: getRecaptchaResponse
});
};
window.renderCaptcha = renderCaptcha;
$.getScript('https://www.google.com/recaptcha/api.js?onload=renderCaptcha&render=explicit', function() {});
},