I'm writing because no question on this site has truly helped me figure out why reCaptcha isn't getting verified. I'm using two reCaptcha's V2 and Invisible on my page. Unfortunately, the project I'm working with is very old and while it looks like it's ASP.NET MVC, I don't see any controllers or models, so I have no idea what it really was written in. I'm rebuilding the project in ASP.NET WebForms, and I have reCaptcha working in it perfectly. The problem is that I have to use JavaScript for the old site until I get the new site published which won't be for several more months.
Here is my form:
<form id="form" action="?" method="post">
<fieldset>
<div class="form-group">
<div class="row">
<div class="col-md-12">
@* RECAPTCHA V2 WIDGET *@
<div id="captcha2" class="g-recaptcha"
data-sitekey="6LdOZWkUAAAAAE1EhMyhMPF5IwM3lflaeyZ2VWaB"></div>
<br />
@* MESSAGE FROM RECAPTCHA *@
<div id="msg" class="msg-error error"></div>
<br>
@* SUBMIT BUTTON FOR VALIDATION & RECAPTCHA *@
<input id="submit" type="submit" name="buttonSubmit" class="btn btn-success btn-large"
value="Submit Information Request" />
<input type="hidden" name="registerDate" value="@DateTime.Now" />
</div>
</div>
</div>
@* RECAPTCHA INVISIBLE WIDGET *@
<div id="captchaI" class="g-recaptcha"
data-sitekey="6LduaWkUAAAAAHp0EU5JXdTIszKR-g2AZfDqmLiv"
data-callback="submit"
data-size="invisible">
</div>
</fieldset>
Here is my JavaScript:
<script>
$('#submit').click(function () {
//var obj =
//{
// secret: "MY_PRIVATE_KEY",
// response: grecaptcha.getResponse()
//};
//var json = JSON.stringify(obj);
//window.location = "https://www.google.com/recaptcha/api/siteverify?" + json;
//var respJSON = '{"success": true | false, "challenge_ts": timestamp, "hostname":string}';
//var respObj = JSON.parse(respJSON);
//alert(respObj.success + "\n" + respObj.challenge_ts + "\n" + respObj.hostname);
//--------------------------------------------------------------------
alert("NO JSON\n" + grecaptcha.getResponse()); //I GET A HASHED RESPONSE
var response = grecaptcha.getResponse();
var stringy = JSON.stringify("STRINGIFY\n" + response);
alert(stringy); //I GET A HASHED RESPONSE
var parsy = JSON.parse("PARSED" + response);
alert(parsed); //I GET NOTHING
//--------------------------------------------------------------------
//var pk = 'MY_PRIVATE_KEY';
//var url = "https://www.google.com/recaptcha/api/siteverify?secret=";
//$.post(url,
// {
// "secret": pk,
// "response": "&response=" + response
// },
// function (response) {
// alert(response);
// });
//alert(response);
//JSON.stringify(response);
//alert(response);
//$('#msg').prepend(response);
//if (!response) {
// $('#msg').text("No Response from Captcha.");
//}
});
As you can see I'm using jQuery with JSON. I've tried to pull the response from the reCaptcha in numerous ways, usually posting messages in the #msg div, but nothing ever gets displayed there.
I know what the Google Developers reCaptcha Guide says, but it is less than helpful on how to actually implement this. Please help as no other solution either verifies the response with siteverify, uses PHP which I cannot use due to agency controls, or as in the case of the Developers Guide, is less than helpful.
//
each line just use/* [Code] */
.