# Redirect being performed on server when via AJAX call?

**URL:** <https://www.ruby-forum.com/t/redirect-being-performed-on-server-when-via-ajax-call/97037>\
**Category:** Rails\
**Created:** [July 4, 2006, 12:12pm UTC](https://www.ruby-forum.com/t/redirect-being-performed-on-server-when-via-ajax-call/97037 "2006-07-04T12:12:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bb73d2/32.png) [@ian](https://www.ruby-forum.com/u/ian)\
**Post date:** [July 4, 2006, 12:12pm UTC](https://www.ruby-forum.com/t/redirect-being-performed-on-server-when-via-ajax-call/97037/1 "2006-07-04T12:12:36Z")

</div>

Hi,

I’m having a hard time understanding why when I make a request via an  
AJAX call that triggers a redirect, the redirect seems to be performed  
by the server and the response of that subsequent request is the one  
sent back to the client.

I am aware I can send back JavaScript in the response to perform the  
location change rather than a redirect but I’d like to have the 302  
response sent to the client, if possible. Prototype.js has the ability  
to bind an event to a 302 status, yet the client only ever gets the 200  
OK response of the request made to the redirect page.

Anyone care to enlighten me?

Many thanks  
Ian

---

<div class="post-metadata">

**Author:** ![ian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bb73d2/32.png) [@ian](https://www.ruby-forum.com/u/ian)\
**Post date:** [July 4, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/redirect-being-performed-on-server-when-via-ajax-call/97037/2 "2006-07-04T17:45:44Z")

</div>

Ian wrote:

> Hi,
> 
> I’m having a hard time understanding why when I make a request via an  
> AJAX call that triggers a redirect, the redirect seems to be performed  
> by the server and the response of that subsequent request is the one  
> sent back to the client.
> 
> I am aware I can send back JavaScript in the response to perform the  
> location change rather than a redirect but I’d like to have the 302  
> response sent to the client, if possible. Prototype.js has the ability  
> to bind an event to a 302 status, yet the client only ever gets the 200  
> OK response of the request made to the redirect page.
> 
> Anyone care to enlighten me?
> 
> Many thanks  
> Ian

OK, I’ve finaly managed to track down what’s going on here (after way  
too long). XMLHttpRequest will transparently follow redirect responses  
(may be different from browser to browser though). So if you’re doing an  
AJAX call to replace a div (or whatever) with the returned content and  
you trigger a redirect you’re going to get the content of the redirect  
page in your div. If you send back some javascript (which Prototype will  
evaluate) to set location.href you’ll get redirected, but you’ll also  
see the text in your div before it does (due to using the :update  
callback on your form). So the way round this is to render whatever you  
need on the backend and send it to the frontend as Json along with some  
status inforation and do your div upadting or redirection there.

Hope this saves someone the time I wasted trying to figure it out 🙂
