我正在使用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