0

How to make below function to return true if success, otherwise false. I have make return on it but the result is 'undefined' when i call it to other javascript on the same page like **if(onloadCallback == false)**. Thank you for helping.

 <script type="text/javascript">
        var onloadCallback = function () {
            grecaptcha.render('dvCaptcha', {
                'sitekey': '<%= ReCaptcha_Key %>',
                'callback': function (response) {
                    $.ajax({
                        type: "POST",
                        url: "product-info.aspx/VerifyCaptcha",
                        data: "{response: '" + response + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {
                            var captchaResponse = jQuery.parseJSON(r.d);
                            if (captchaResponse.success) {
                                $("[id*=txtCaptcha]").val(captchaResponse.success);
                                $("[id*=rfvCaptcha]").hide();
                                return true;
                            } else {

                                $("[id*=txtCaptcha]").val("");
                                $("[id*=rfvCaptcha]").show();
                                var error = captchaResponse["error-codes"][0];
                                $("[id*=rfvCaptcha]").html("ReCaptcha error. " + error);
                                return false;
                            }
                        }
                    });
                }
            });
        };
    </script> 
3
Contributors
2
Replies
22
Views
1 Year
Discussion Span
Last Post by AleMonteiro
0

Since you have not had any replies I will see if I can help (I do not use jQuery so cannot check the detailed syntax). As far as I can tell the organisation of your code is OK and if it runs without errors then it should return either true or false.
So I wonder if the code is failing somewhere which is why it returns undefined.
Try puting in a few diagnostic alert boxes to see where your code "goes" (or does not go). Also turn on whatever error reporting that your browser provides.

0

You can't. But let me explain.

First, the function onloadCallback does not have any return. You can make it return true or false, but this value will never respect the ajax result.

When you call onloadCallback you get the return right away, because it's returns before the ajax is completed. Once the ajax is completed the return inside the 'success' callback will not be return to whoever called onloadCallback.
To make this work you need to add a callback parameter on onloadCallback.

Something like this

var onloadCallback = function (pCallBack) {
            grecaptcha.render('dvCaptcha', {
                'sitekey': '<%= ReCaptcha_Key %>',
                'callback': function (response) {
                    $.ajax({
                        type: "POST",
                        url: "product-info.aspx/VerifyCaptcha",
                        data: "{response: '" + response + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {
                            var captchaResponse = jQuery.parseJSON(r.d);
                            if (captchaResponse.success) {
                                $("[id*=txtCaptcha]").val(captchaResponse.success);
                                $("[id*=rfvCaptcha]").hide();
                                pCallBack.call(pCallBack, true); // callback true
                                // return true; // this return won't do a thing
                            } else {
                                $("[id*=txtCaptcha]").val("");
                                $("[id*=rfvCaptcha]").show();
                                var error = captchaResponse["error-codes"][0];
                                $("[id*=rfvCaptcha]").html("ReCaptcha error. " + error);
                                pCallBack.call(pCallBack, false); // callback false
                                // return false; // this return won't do a thing
                            }
                        }
                    });
                }
            });
            return false; // This would be the return of the function, but here you don't know the result yet.
        };
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.