jquery帖子行为帮助

时间:2010-12-21 00:16:21

标签: jquery asp.net-mvc

它的意思是调用一个名为Logon的Action方法。 取决于我希望它重定向到网址的逻辑,或者设置一些错误文本。

它以json数据的形式获取正确的信息,但是当我点击提交按钮时,它所做的就是在新选项卡中显示json结果。

这里有什么问题?

$("#submit").click(function () {
                $.post({
                    url: "Account/LogOn",
                    dataType: "json",
                    success: function (data) {
                        if (data.redirect) {
                            // data.redirect contains the string URL to redirect to
                            window.location.href = data.redirect;
                        }
                        else {
                            // data.form contains the HTML for the replacement form
                            $("#error").replaceWith(data.error);
                        }
                    }
                });

2 个答案:

答案 0 :(得分:1)

您需要取消默认操作...

return false;处理程序末尾的

click

$("#submit").click(function () {
                $.post({
                    url: "Account/LogOn",
                    dataType: "json",
                    success: function (data) {
                        if (data.redirect) {
                            // data.redirect contains the string URL to redirect to
                            window.location.href = data.redirect;
                        }
                        else {
                            // data.form contains the HTML for the replacement form
                            $("#error").replaceWith(data.error);
                        }
                    }
                });
                return false;
});

答案 1 :(得分:-1)

为什么你在客户端而不是服务器端这样做?

在服务器端,您可以重定向或重新显示包含错误的登录表单。

 return Redirect(returnUrl);
return View(model); //model contains the Model Errors. It can be added using ModelState.AddModelError(...);