简体   繁体   中英

asp.net response.redirect hitting url, but not changing browser page

I have an aspx page (webforms) that is called from a jQuery Post method (which works fine), however the Response.Redirect method from the code behind does not reload the browser with the redirected URL. It does actually hit the URL, however. I'm pretty sure this has something to do with the page being called from jQuery, but not sure why this behavior is happening.

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //lid = Validation.StripToGUID(ObjToGUID(Request.QueryString("lid")))
        lid = Validation.StripToGUID(ObjToGUID(Request.Form["lid"]));
        string sid = null;
        if (lid.Length == 36)
        {
            //retrieve the link (and product data) from the database
            LiveItem_Data.DBConnStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            LiveItem o = new LiveItem();
            o = LiveItem_Data.GetLive_ItemFromLID(lid);


            if (HTTPStuff.RemoteFileExists(o.product_link))
            {
                Response.Redirect(o.product_link, true);
            }
            else
            {
                //generate an error                 
            }


        }

    }
}

I stepped through the code and the product_link is working (manually cutting and pasting into a browser), and the remote page is being called (I'm testing the link with another site that has logging). The browser however does not open (tried FF, IE, Opera, Chrome) the new URL.

The jQuery post:

$('.popInfoHG').click(function () {
    var ListID = $(this).parent().find('[id*=hidlid]').val();
    $.post("redir.aspx", { lid: ListID });
});

I verified that HTTPredirect feature is turned on in IIS Express (from Visual Studio 2012). Stumped!

I think from the jquery function, it will not redirect the page from server you will have to do this on client side itself ie in the success part of the click function

$('.popInfoHG').click(function () {
    var ListID = $(this).parent().find('[id*=hidlid]').val();
    $.post("redir.aspx", { lid: ListID }).success(function() {

//write the redirection code here

});
});

I don't think you're using post right. Post is for making a hidden service call and does not affect the browser window.

If you just want to redirect the browser window, and redir.aspx will accept either GET or POST, you can use window.location.href:

$('.popInfoHG').click(function () {
    var ListID = $(this).parent().find('[id*=hidlid]').val();
    window.location.href = "redir.aspx?lid=" + ListID;
});

If redir.aspx does not accept GET, I suggest you create a hidden form on your page (with an lid hidden input element) and post it programmatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM