DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Performing A Redirect With AJAX

  • submit to reddit
        If a redirect is returned from the rails' form_remote_tag or link_to_remote methods, a message is displayed saying something like "You are being redirected", where "redirected" is a link the user must click.  The code snippet below will actually redirect the user to the proper location, without them having to click the link.  Code works for both form_remote_tag or link_to_remote methods.

<% link_to_remote "Foo",
                  :url => { :action => "foo" },
                  302 => "document.location = request.getResponseHeader('location')" %>

Or, an example from the Typo source:
<%= form_remote_tag :url => {:action => "comment", :id => @article}, 
                    :update => {:success => 'commentList'},
                    :loading => "loading()", 
                    :complete => "complete(request)",
                    :failure => "failure(request)",
                    :html => {:id=>"commentform",:class=>"commentform"},
	            302 => "document.location=request.getResponseHeader('location')"%>


Snippets Manager replied on Tue, 2006/12/05 - 3:14pm

It's a nice idea but it doesn't work for me. Instead, the redirect happens within the element I'm trying to update so I get a "site within a site" effect. Maybe Prototype was updated to handle 302 itself?