AJAX MVC3 jquery ajax 回调总是在成功时进入
我有以下问题,我有一个由 ajax 调用的 post 操作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AX_Login(LoginVM usersubmitted)
{
if (ModelState.IsValid)
{
return Json(new { success = true });
}
else
{
return Json(new { success=false, error = true });
}
}
另一方面,我有以下按钮的 jquery 处理程序:
$("#Jquery_LoginButton").click(
function () {
$.ajax({
type: "POST",
url: "@(Url.Action("AX_Login","Users"))",
data:$("#MiniLoginForm").serialize(),
success: function (result)
{
alert("Good");
},
error: function ()
{
alert("Bad");
}
});
}
)
我的问题:已达到操作,但 ajax 调用总是成功结束 - 即使模型无效-。
问题:
如果我设置为 false 成功,您知道为什么会发生这种情况吗?如果模型无效,我可能会抛出异常,但我看不到优雅的东西。
您如何正常管理数据注释和 Ajax 的验证?
im having the following issue i have this post action that is called by ajax:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AX_Login(LoginVM usersubmitted)
{
if (ModelState.IsValid)
{
return Json(new { success = true });
}
else
{
return Json(new { success=false, error = true });
}
}
For other hand i have the following jquery handler for a button:
$("#Jquery_LoginButton").click(
function () {
$.ajax({
type: "POST",
url: "@(Url.Action("AX_Login","Users"))",
data:$("#MiniLoginForm").serialize(),
success: function (result)
{
alert("Good");
},
error: function ()
{
alert("Bad");
}
});
}
)
My Issue: The action is reached, but always the ajax call ends on sucess - even if the model is not valid -.
Questions:
Do you know why this is happening if im setting to false the success? i could throw an exception if the model is not valid but i dont see the thing elegant.
How do you manage normally the validation with Data Annotations and Ajax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你总是会获得“成功”,这是正确的,因为你的要求
总是工作。
$.ajax
不知道返回什么数据。因此,只有当您的请求失败(例如 404)时,您才会收到
错误
,您必须执行此操作
希望这会有所帮助
It is right that you always get "success" because your request
always work.
$.ajax
doesnt know what data comes back.So you will get
error
only if your request failes (404 for example)you have to do this
hope this helps