在MVC视图中,
一个提交按钮,我在其中检查所有验证。如果验证错误,则返回false。这样就不会发布该表格。
但是我正在使用Ajax get方法检查重复检查。如果该值存在,它将返回true。然后,对于按钮提交,我将返回false。
但是在我的情况下,ajax调用未返回任何内容。或者更确切地说,该过程不会等待ajax回来。无论如何我能实现它。
单击我的代码按钮
var isDraftValid = 0;
if(checkAjax()==false)
{
isDraftValid++;
}
if(check2()==false)
{
isDraftValid++;
}
if(isDraftValid>0)
{
return false;
}
CheckAjax代码:
var href = '@Url.Action("IsDuplicate", "Book")' + '/' + bTitleVal;
$.ajax({
type: "get",
url: href,
dataType: "json",
traditional: true,
cache: false,
contentType: 'application/json',
//data: JSON.stringify({ bookTitle: bTitleVal }),
data: {},
success: function (returndata) {
debugger;
if (returndata == true) {
return true;
}
else {
return false;
}
},
error: function (arg, data, value) {
}
});
您的ajax请求是异步的,因此您必须使用回调进行此操作,或者(不建议)添加async:false:
$.ajax({
type: "get",
url: href,
async: false
再详细一点,在CheckAjax方法中声明结果变量,并在请求完成时为其分配值,然后返回此变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句