I'm trying to add a captcha to a VB.net page. The only explanation is Uncaught (in promise) null? I added a label which I have in the script but wasn't on the page and the error goes away but it still doesn't work? By the way, I do get the Are you a robot message if I don't check but if I do check, it just reloads the page and doesn't continue the code in the button click event?
My code:
Script:
var your_site_key = 'xxxxxxxxxxxxxxxxxx';
var renderRecaptcha = function () {
grecaptcha.render('ReCaptchContainer', {
'sitekey': 'xxxxxxxxxxxxxxxxxx',
'callback': reCaptchaCallback,
theme: 'light', //light or dark
type: 'image',// image or audio
size: 'normal'//normal or compact
});
};
var reCaptchaCallback = function (response) {
if (response !== '') {
document.getElementById('lblMessage1').innerHTML = "";
}
};
Design view:
<div id="ReCaptchContainer"></div>
<br/><asp:Label ID="lblMessage1" runat="server" />
Code Behind:
In button click
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
If IsReCaptchValid() Then
Else
lblMessage1.Text = "Are you a robot?"
Return
End If
Function:
Public Function IsReCaptchValid() As Boolean
Dim result = False
Dim captchaResponse = "g-recaptcha-response"
Dim secretKey = "xxxxxxxxxxxxxxxxxxx"
Dim apiUrl = "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}"
Dim requestUri = String.Format(apiUrl, secretKey, captchaResponse)
Dim request = CType(WebRequest.Create(requestUri), HttpWebRequest)
Using response As WebResponse = request.GetResponse()
Using stream As StreamReader = New StreamReader(response.GetResponseStream())
Dim jResponse As JObject = JObject.Parse(stream.ReadToEnd())
Dim isSuccess = jResponse.Value(Of Boolean)("success")
result = If((isSuccess), True, False)
End Using
End Using
Return result
End Function