0

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 the response body. So my guess is that the .ashx gets redirected to the .aspx.

void ProcessRequest(HttpContext context)
{
     context.Response.Redirect(newUrl);
}

How do I redirect the user from the .aspx page that sends the ajax request? Do I need to call a method on that .aspx from my handler.ashx?

Community
  • 1
  • 1
Johan
  • 35,120
  • 54
  • 178
  • 293

1 Answers1

1

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");
    }
});
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thanks, but is there any way to refer to the aspx httpcontext from the ashx file so that i can make the transfter in the server code? – Johan Apr 04 '13 at 12:02
  • @Johan You do not make any transfer in the server code. The server code is only write and send some html page, that include some headers that make the transfer. You believe that the transfer is made on server because simple the browser did not show you the middle page. – Aristos Apr 04 '13 at 12:04
  • @Johan If you handler did not return any data on the ajax, to make the redirect work, just call it as any page without ajax. Eg as link, or with javascript you can call it with the window.location.replace()... etc (not ajax and the redirect work) – Aristos Apr 04 '13 at 12:05
  • The problem is that the call should be made on a click of a `confirm()` dialog. Thats why I'm using ajax. But I guess client side transfer is the way to go. Thanks – Johan Apr 04 '13 at 12:13
  • @Johan On the confirm, just call it without ajax, is the same - try it. – Aristos Apr 04 '13 at 12:14
  • How should I call a serverside method from javascript without ajax? – Johan Apr 04 '13 at 12:19
  • @Johan Server side no, a handler as you say yes, – Aristos Apr 04 '13 at 12:30
  • @Johan call it with javascript as: `window.location.replace("yourhandler.ashx");` – Aristos Apr 04 '13 at 12:41