Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Performing A Redirect With AJAX

DZone's Guide to

Performing A Redirect With AJAX

·
Free Resource
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')"%>
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}