jQuery阻止RedirectToAction工作? [英] jQuery preventing RedirectToAction from working?

查看:129
本文介绍了jQuery阻止RedirectToAction工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户登录成功,我正在尝试将其重定向,但是我页面上的代码似乎阻止了重定向工作.如果我删除下面的jQuery,则重定向有效.有人可以告诉我我做错了什么吗?谢谢

I'm trying to redirect the user if they login successfully but the code I have on my page seems to be preventing the redirection from working. If I remove the jQuery below the redirection works. Can somebody tell me tell me if there's something I'm doing wrong? Thanks

我有以下操作:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(User user)
        {
            var myErrors = new Dictionary<string, string>();
            try
            {
                if (ModelState.IsValid)
                {
                    if (userRepository.ValidUser(user))
                    {
                        return RedirectToAction("Index", "Group", new {page = (int?)null});

                    }
                    else
                {
                        return Json("Username or password seems to be incorrect");
                    }
                }
                else
                {
                    foreach (KeyValuePair<string, ModelState> keyValuePair in ViewData.ModelState)
                    {
                        if (keyValuePair.Value.Errors.Count > 0)
                        {
                            List<string> errors = new List<string>();

                            myErrors.Add(keyValuePair.Key, keyValuePair.Value.Errors[0].ErrorMessage);
                        }

                    }
                    return Json(myErrors);
                }
            }
            catch (Exception)
            {
                return Json("Invalid");
            }

        }

以及我页面上的以下代码:

and the following code on my page:

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $("#SaveSuccess").hide();
        $("#btnLogin").click(function() {

            $("form").submit(function(event) {
                var formData = $(this).serialize();
                $.post($(this).attr("action"), formData, function(res) {
                    ShowErrors(res); 
                    if (res == true) {

                        $("#SaveSuccess").text("Saved");
                    }
                    else {
                        $("#divError").html(res);
                    }
                    $("#SaveSuccess").fadeIn(100);
                }, "json");
                return false;
            });
        });
    });
</script>

推荐答案

您应该看看

You should have a look at this question, your issue is that it's sending a 302 (IIRC, it's early) in response to the AJAX request, not an overall page request, so you need to look for that header and redirect the entire page if it's found.

最佳答案中的方法可能会变得跨浏览器一样整洁,只需返回带有重定向URL的JSON并重定向到该URL,如下所示:

The approach in the top answer there is probably as neat as it gets cross-browser, just return JSON with a redirect url and redirect to that url, like this:

        $("form").submit(function(event) {
            var formData = $(this).serialize();
            $.post($(this).attr("action"), formData, function(res) {
                if (res.redirect) {
                  window.location.href = res.redirect;
                  return;
                }
                ShowErrors(res); 
                if (res == true) {
                    $("#SaveSuccess").text("Saved");
                }
                else {
                    $("#divError").html(res);
                }
                $("#SaveSuccess").fadeIn(100);
            }, "json");
            return false;
        });

在C#端,类似这样的东西,而不是重定向:

On the C# Side something like this instead of the redirect:

return JSon({redirect = Url.Action("Index", "Group", new {page = (int?)null})});

这篇关于jQuery阻止RedirectToAction工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆