Redirect from .ashx to .aspx using $.ajax()

后端 未结 1 1504
执念已碎
执念已碎 2021-01-28 01:28

If I use the solution posted here, I won\'t get transferred to the new page. But if i inspect the $.ajax() call in the console, the actual .aspx page seems to be in

相关标签:
1条回答
  • 2021-01-28 02:02

    The Response.Redirect command is nothing more than instructions to the browser to move to some other page. This commands are first a header instruction to the browser. Also there is a javascript small script that is written on the page in case the header is fail.

    Now, when you make the call with Ajax (no matter if its handler or page) the results return to the page but inside the ajax buffer, the redirect headers are "eaten" and not go to the browser itself to runs them. So no redirect can be directly done via ajax call.

    What you should do, is to return a signal to your ajax call, and see that signal/flag and make the redirect using javascript call window.location.replace("redirectPage.aspx")

    For example, something like:

    $.ajax({
        url: "action.ashx",
        type: "GET",
        dataType: 'json',
        success: function(data)     
        {
            if(data.NeedRedirect)
                window.location.replace("WhereToMove.aspx");
        },
        error: function(responseText, textStatus, XMLHttpRequest) 
        {
            window.location.replace("WhereToMove.aspx");
        }
    });
    
    0 讨论(0)
提交回复
热议问题