【问题标题】:Response.Redirect does not change url c#Response.Redirect 不会改变 url c#
【发布时间】:2013-06-07 14:29:44
【问题描述】:

我正在使用 jQuery 和 jQueryMobile 框架构建 asp .net 网站。成功登录后,我可以看到下一页的内容,但 URL 保持不变,即/Login.aspx

当我按 F5 时,只有 URL 发生变化。

登录.aspx

<div data-role="content">
    <form id="frmLogin" method="post" runat="server" action="Login.aspx">
        <div data-role="fieldcontain">
            <input type="text" name="txtUserName" id="txtUserName" placeholder="User Name" value="" runat="server" /><br />
            <input type="password" name="txtUserPass" id="txtUserPass" placeholder="Password" value="" runat="server" />
            <br />          
            <button id="cmdLogin" type="button">Login</button>
        </div>
    </form>
    <div id="divDialog"></div>
</div>

点击cmdLogin登录按钮时调用的JavaScript

$('#cmdLogin').click(function () {
        $.ajax({
        url: 'ajaxExecute.aspx?Fn=VUSR',
        type: 'POST',
        context: document.body,
        data: 'User=' + $('#txtUserName').val() + '&Pass=' + $('#txtUserPass').val(),
        cache: false,
        success: function (response) {
            alert(f);
            if (response == '1') {                    
                f.submit();
            }
            else {
                /*
                Print Error
                */
            }
        }
    });
});

后面的登录代码

登录.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {       
        routeToDefaultPage();
    }   
}
private void routeToDefaultPage()
{
    Response.Redirect("Piechart.aspx");
}

这里有什么问题?

当我在登录成功后检查元素时(Piecharts.aspx 的内容但 URL 是 Login.aspx)。我在头部看到以下内容

<base href="http://localhost:49712/Login.aspx">

【问题讨论】:

  • 换个浏览器试试看有没有变化
  • 它是一个移动网站...我在 Chrome 和 Android 浏览器中尝试过..
  • 它适用于移动设备的opera mini网络浏览器。

标签: c# asp.net jquery jquery-mobile


【解决方案1】:

您确定在单击时将标志 ispostback 分配为 true。这条线的断点可以帮助你吗?

【讨论】:

  • 是的,当您单击“登录”按钮时,断点就会命中……即使是 function routeToDefaultPage() 也会被调用。这就是为什么我能够看到Piecharts.aspx 页面的内容
  • 对不起,我不明白您被重定向到正确页面但 URL 没有改变的问题。您是否尝试使用 Response.Redirect("/Piecharts.aspx"); 重定向到绝对路径?您也可以尝试在重定向之前清除缓存 Response.Clear(); ?
【解决方案2】:

试试这个:

Response.Redirect("Piechart.aspx");
Response.End();

【讨论】:

    【解决方案3】:

    ajax 调用后 jquery 重定向怎么样?

    尝试在ajax调用成功后添加这个。 header( 'Location: Piechart.aspx' );?

    $('#cmdLogin').click(function () {
            $.ajax({
            url: 'ajaxExecute.aspx?Fn=VUSR',
            type: 'POST',
            context: document.body,
            data: 'User=' + $('#txtUserName').val() + '&Pass=' + $('#txtUserPass').val(),
            cache: false,
            success: function (response) {
                // redirects page after login successful
                header( 'Location: Piechart.aspx' );            
    
                alert(f);
                if (response == '1') {                    
                    f.submit();
                }
                else {
                    /*
                    Print Error
                    */
                }
            }
        });
    });
    

    【讨论】:

      【解决方案4】:

      这对我有用...!!

      $.mobile.changePage( "/Piecharts.aspx", {
        transition: "pop"
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-25
        • 1970-01-01
        相关资源
        最近更新 更多