我正在使用ASP.Net MVC并尝试将Google reCaptcha对象实现为页面。ASP.Net MVC Recaptcha Jquery Ajax问题
我想避免在我的表单中使用模型,并且只想直接调用使用jquery ajax的方法。
我有验证码出现,但是在调试器中检查RecaptchaVerificationHelper对象时,输入的任何内容都显示为空。
任何建议,以保持它像我拥有它轻量级,但保持它的工作。
注意:大多数逻辑已被剥离出来,只是试图让验证码逻辑工作。
CSHTML样品:
@using Recaptcha.Web.Mvc;
<script type="text/javascript">
function createUser() {
$.ajax({
type: "POST",
url: 'CreateUser',
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response.Success == true) {
alert("success");
//redirectSuccess();
} else {
alert("failed");
}
},
error: function (err) {
commonError(err);
}
});
}
</script>
@Html.Recaptcha(publicKey: "6LdxcPgSAA...", theme: Recaptcha.Web.RecaptchaTheme.Clean);
<br />
<input type="button" value="Submit" onclick="createUser();" style="margin-right:300px;" />
CS服务器代码示例:
public ActionResult User()
{
return View();
}
public JsonResult CreateUser()
{
Wrapper.ValidationResponse response = new Wrapper.ValidationResponse();
response.Success = true;
RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (String.IsNullOrEmpty(recaptchaHelper.Response))
{
response.Success = false;
}
RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
response.Success = false;
}
try
{
//removed logic
return Json(response);
}
catch (Exception ex)
{
response.Success = false;
response.Message = "Failed to create new user. Please contact us if the issue persists.";
return Json(response);
}
}
由于事先
的NuGet **谷歌验证码V2 **为MVC 4和5 - [NuGet包(HTTPS://www.nuget。 org/packages/reCAPTCH.MVC /) - [Demo And Document](http://recaptchamvc.apphb.com/) – Sender