ReCaptcha: “The bind parameter must be an element or id”

做~自己de王妃 提交于 2019-12-25 01:44:45

问题


ReCaptcha has stopped working on our knockout site.

I get the following error in the console:

Uncaught Error: The bind parameter must be an element or id
    at kr (recaptcha__en.js:369)
    at nr (recaptcha__en.js:373)
    at Object.or [as render] (recaptcha__en.js:374)
    at loadReCaptcha (KnockoutBindings.js:135)
    at KnockoutBindings.js:143

I have a custom binding to handle the ReCaptcha.

 <div id="reCaptcha" data-bind="reCaptcha: {key: 'my-key', callback: reCaptchaResponse}"></div>

Handled by:

ko.bindingHandlers.reCaptcha = {
    init: function (element, valueAccessor) {

        var val = ko.utils.unwrapObservable(valueAccessor()),
            key = ko.utils.unwrapObservable(val.key),
            callback = val.callback;                     

        function loadReCaptcha() {
            if (typeof grecaptcha !== "undefined") {
                grecaptcha.render(element.id, {
                    'sitekey': key,
                    'theme': 'light',
                    'callback': callback
                });
            }
            else {
                setTimeout(function () {
                    loadReCaptcha();
                }, 150);
            }
        }

        loadReCaptcha();
    }
};

This was previously working but has stopped working recently.

I have checked:

  • The key is still valid
  • The reCaptcha div is loaded and visible
  • Tried changing the Id/using another div
  • Tried passing the element instead of the id

Googling the exact error "The bind parameter must be an element or id" only gives links to the line in the recaptcha.js file.

I cannot find any information on what the error actually means or how to resolve it.

I am also getting a message in the bottom right corner of the page "This site key is not enabled for the invisible captcha." But I believe this is a side effect of the reCaptcha not loading.


回答1:


I had exactly the same issue. It turns out that the problem is actually the "data-bind" attribute. Not sure why it "stopped" working, but I'm assuming google will probably introduce a new property with the name "bind".

I changed my binding to create a div within the element, thus ensuring that the element has no data attributes at all.

If you change your binding to this, it should work:

ko.bindingHandlers.reCaptcha = {
    init: function (element, valueAccessor) {

        var val = ko.utils.unwrapObservable(valueAccessor()),
            key = ko.utils.unwrapObservable(val.key),
            callback = val.callback;

        function loadReCaptcha() {
            if (typeof grecaptcha !== "undefined") {
                var $target = $('<div />').appendTo($(element));
                grecaptcha.render($target[0], {
                    'sitekey': key,
                    'theme': 'light',
                    'callback': callback
                });
            }
            else {
                setTimeout(function () {
                    loadReCaptcha();
                }, 150);
            }
        }

        loadReCaptcha();
    }
};


来源:https://stackoverflow.com/questions/46657573/recaptcha-the-bind-parameter-must-be-an-element-or-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!