【发布时间】:2016-04-25 10:00:16
【问题描述】:
我在一个页面上有两个 ajax 表单,它们都是部分视图。两者都有recaptcha,我似乎无法在表单提交且验证失败后找到recaptcha 重新加载的方法。
Recaptcha 是显式加载的,我有两个问题,首先如果我使用:
<script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
在这两种表单上,我都会收到“未捕获的错误 recaptcha 占位符元素必须为空”。所以我不能那样做。如果我将上面的内容与整个页面的整体布局一起加载,我会遇到第二个问题,即一旦表单提交不正确,recpatcha 将不会重新加载。
加载每个recaptcha的脚本如下:
var recaptcha1;
var recaptcha2;
var myCallBack = function () {
//Render the recaptcha1 on the element with ID "recaptcha1"
recaptcha1 = grecaptcha.render('recaptcha1', {
'sitekey': 'sitekeyhere',
'theme': 'light'
});
//Render the recaptcha2 on the element with ID "recaptcha2"
recaptcha2 = grecaptcha.render('recaptcha2', {
'sitekey': 'sitekeyhere',
'theme': 'light'
});
表格中的recaptcha如下所示:
<div id="recaptcha1"></div>
我使用的是 MVC 5,我的表单提交的控制器操作是这样的:
public async System.Threading.Tasks.Task<ActionResult> FormSubmit(contactform model, string whichpage)
{if (ModelState.IsValid && status)
{*Updates database/sends email here*
else
{
return PartialView("_contactform", model);
}
我尝试了多种组合,将不同的东西加载到不同的位置,但我找不到一个没有错误的组合。当任一表单已提交且验证失败时,我可以在各自的表单上同时使用两种验证码的最佳方式是什么?
【问题讨论】:
标签: javascript jquery ajax asp.net-mvc forms