我想在我的MVC的网站使用reCAPTCHA,但它不验证,除非我从一个表单提交,就像这样:提交数据VS数据从阿贾克斯
@using(Html.BeginForm("VerifyCaptcha", "Signup"))
{
@ReCaptcha.GetHtml(theme: "clean", publicKey: "6LcnfAITAAAAAAY--6GMhuWeemHF-rwdiYdWvO-9");
<input type="submit" id="btnVerify" value="Verify" />
}
[HttpPost]
public ActionResult Index(PolicyModel model)
{
var result = ReCaptcha.Validate(privateKey: "THE_KEY");
return View();
}
我不不想使用表单提交,因为我不想返回新的视图。我所有的数据都以json形式被ajax推送。我想要做的是:
$.ajax({
url: 'verifyCaptcha',
dataType: 'json',
contentType: "application/x-www-form-urlencoded",
type: "POST",
async: false,
success: function (response) {
alert(response);
},
error: function(response) {
alert('There was a problem verifying your captcha. Please try again.');
}
});
return valid;
[HttpPost]
public ActionResult VerifyCaptcha()
{
var result = ReCaptcha.Validate(privateKey: "THE_KEY");
return Json(result);
}
AJAX调用获取到控制器,但验证方法立即完成,仿佛它甚至没有得到提出要求。我不确定为什么验证总是失败,如果验证码不在表单中 - 它是否会丢失信息,如公钥或什么?有没有解决这个问题的方法?
编辑:添加了没有模型的ajax控制器操作方法。
更改按钮类型为按钮,并在您的ajax分配一个有效的网址,在您的按钮单击事件 – 2015-02-23 13:16:11
内写入ajax,所以你在你的ajax代码中设置'type:'POST''?让你的代码处于完成状态,更容易帮助你 – sabotero 2015-02-23 13:22:21
ok,你的ajax请求到达'Action'或者没有? – sabotero 2015-02-23 13:23:41