【发布时间】:2015-02-24 08:01:34
【问题描述】:
我正在使用 ASP.Net MVC 并尝试将 Google reCaptcha 对象实现到页面中。
我试图避免在我的表单中使用模型,并且只想使用 jquery ajax 直接调用方法。
我已显示验证码,但在调试器中检查 RecaptchaVerificationHelper 对象时,我输入的任何内容都显示为 null。
任何建议让它像我一样保持轻量级,但要让它继续工作。
注意:这里的大部分逻辑已经被剥离,只是试图让验证码逻辑正常工作。
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 Google reCAPTCHA V2 适用于 MVC 4 和 5 - NuGet Package - Demo And Document
标签: jquery html ajax asp.net-mvc recaptcha