【问题标题】:Page redirection not working for mvc in ajax success页面重定向不适用于 ajax 成功中的 mvc
【发布时间】:2016-09-28 13:53:03
【问题描述】:

从 ajax 调用中成功检索数据。 在 ajax 成功中,我需要重定向到“/Account/Index”,但重定向在 ajax 成功中不起作用。 window.location.href 有什么替代方案吗?

$.ajax({        
            url: "/Register/Register",
            async: false,
            type: "POST",
            data: JSON.stringify(RegisterMember),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (s) {
                console.log(s);
                //LoadprojectsGrid();
                // End For


                if (s.MessageCode == 1) {
                    event.preventDefault();
                    window.location.href = '/Account/Index';
                }
                else if (s.MessageCode == 2) {
                    $('.cssload-loader').remove();
                    $('#overlay').remove();
                    $.growl.error({ message: s.Message });

                }
                 else
                 {
                    $('.cssload-loader').remove();
                    $('#overlay').remove();
                    $.growl.error({ message: s.Message });

                }


            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
                $('.cssload-loader').remove();
                $('#overlay').remove();
            }
        });

【问题讨论】:

  • window.location.href='newUrl' 应该可以工作。你确定你的 if 条件返回true
  • 是条件返回真
  • 根据 W3C:window.location.href 属性返回当前页面的 URL。
  • 您可以为该属性设置一个新的 url 以进行重定向。多年来一直在使用它,并且在任何浏览器中都没有遇到过问题!

标签: javascript jquery asp.net-mvc asp.net-mvc-4


【解决方案1】:

没有href:

window.location = '/帐户/索引';

【讨论】:

【解决方案2】:

根据一些数据我发现应该是

window.location 

而不是

window.location.href

http://www.w3schools.com/js/js_window_location.asp

【讨论】:

    【解决方案3】:

    尝试如下使用 URL 助手:

    window.location.href = '@Url.Action("Index", "Account")';
    

    【讨论】:

      【解决方案4】:

      删除

      event.preventDefault(); 
      

      并使用

      window.location = '/Account/Index'; as stated by @marcos
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-31
        • 2016-08-07
        • 1970-01-01
        相关资源
        最近更新 更多