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> 

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.

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.
        };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.