简体   繁体   English

Even with the inline JavaScript function - Google ReCAPTCHA couldn't find user-provided function: function (response) -

[英]Even with the inline JavaScript function - Google ReCAPTCHA couldn't find user-provided function: function (response) -

I am using ReCAPTCHA version V2.我正在使用 ReCAPTCHA 版本 V2。 On the callback function (ie data-callback ), I am getting the following error message.在回调 function (即data-callback )上,我收到以下错误消息。

ReCAPTCHA couldn't find user-provided function: function (response) ReCAPTCHA 找不到用户提供的 function:function(响应)

Now, most of the posts/solutions I see on the web are related to a local callback function that doesn't get invoked when referred in data-callback attribute of the g-recaptcha div.现在,我在 web 上看到的大多数帖子/解决方案都与本地回调 function 相关,该回调在g-recaptcha div 的数据回调属性中引用时不会被调用。 However, in my case, even the inline function does not get invoked.但是,就我而言,即使是内联 function 也不会被调用。 Please have a look at the following image.请看下面的图片。

Screenshot-1:截图 1: 截图-1

In fact, when I am using JavaScript native function such as alert(), it is still not working.其实我在使用JavaScript原生function比如alert()的时候,还是不行。

Screenshot-2:截图 2: 截图-2

Here's the JS code I am using.这是我正在使用的 JS 代码。

<script src='https://www.google.com/recaptcha/api.js'></script>

First try - callback function:第一次尝试-回调function:

<div class="g-recaptcha" data-sitekey="Please add your site key if you want to test" data-callback="function (response) { alert('working: ', response);}"></div>

Second try - callback function:第二次尝试 - 回调 function:

<div class="g-recaptcha" data-sitekey="Please add your site key if you want to test" data-callback="Window.alert('hi');"></div>

I appreciate your help if you can help me understand why is the google API responding in a completely weird way.如果您能帮助我理解为什么 google API 以一种完全奇怪的方式响应,我将感谢您的帮助。

Inline JavaScript function in Google ReCAPTCHA will never work. Google ReCAPTCHA 中的内联 JavaScript function 将永远无法工作。

If it could save somebody's time, I am posting his answer here.如果它可以节省某人的时间,我会在这里发布他的答案。

All credit goes to @Christos Lytras .所有功劳归于@Christos Lytras Many thanks to him for helping me understand the JS behind the Google ReCAPTCHA.非常感谢他帮助我理解了 Google ReCAPTCHA 背后的 JS。 What he said in the comment section about Recaptcha's JS tries to identify the function by its name in the global window object, is absolutely correct.他在评论部分所说的关于 Recaptcha 的 JS 试图通过它在全球 window object 中的名称来识别 function,是绝对正确的。 Thus, my implementation was not working and will never work (at least in the V2 version).因此,我的实现不起作用并且永远不会起作用(至少在 V2 版本中)。

In all my solutions when I was trying to implement an inline function, it was read as the window[function (){}] or window[Window.alert('hi');] which is incorrect JS syntax.在我尝试实现内联 function 的所有解决方案中,它被读取为window[function (){}]window[Window.alert('hi');]这是不正确的 JS 语法。 Therefore, when I tried it the following way, it worked like charm.因此,当我按照以下方式尝试时,它就像魅力一样。

Correct approach正确的方法

<script>window.myCallBackFunction = function() { alert("HI"); }</script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="XXX" data-callback="myCallBackFunction" ></div>

Please note: Just for more clarity, I have also tried implementing the callback function initially before posting this question and it didn't work because of the order of the scripts.请注意:为了更清楚起见,我还尝试在发布此问题之前最初实施回调 function,但由于脚本的顺序,它没有工作。 Thanks to this answer on another question that helped me immensely but after @Christos Lytras 's explaination.感谢在@Christos Lytras的解释之后对另一个问题有很大帮助的答案 In the beginning, I was implementing it in the following order.一开始,我是按以下顺序实施的。

Incorrect approach不正确的方法

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>window.myCallBackFunction = function() { alert("HI"); }</script>
<div class="g-recaptcha" data-sitekey="XXX" data-callback="myCallBackFunction" ></div>

I hope it could help somebody like me, in the future.我希望它可以帮助像我这样的人,在未来。

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

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