{ height: 1%; } - Ruby on Rails and User Interface Design

CSS, UI Design, Ruby on Rails and cheese ... lots of cheese

HTTP 302 Redirects + XMLHttpRequests = ARGH!!1

Posted by Richard White Mon, 10 Apr 2006 21:59:40 GMT

Best I can tell the problem is that XMLHttpRequests handle 302 redirects exactly the same way that a regular request would, which in most cases results in that redirect destination page being embedded into an element on the existing page. According to the spec the user agent, aka your browser, will automatically fetch the redirected page and return to you what looks like a perfectly valid response. Therefore you have no knowledge that this is not the page you originally asked for and no way to figure that out.

I’ve seen a code snippet that uses 302 status code handler with your Prototype Ajax requests to set the window location. I’m not sure how that code ever worked, since all my testing shows that you will be redirected silently without the 302 handler ever being called.

The best and only solution I could come up with was to override the default Rails redirect_to method to return javascript (using the PrototypeHelper in Rails 1.1) to redirect the current page using the document.location property:

class ActionController::Base
  alias_method :base_redirect_to, :redirect_to

  def redirect_to(options={}, *parameters_for_method_reference)
    if request.xhr?
      redirect_url = url_for(options, *parameters_for_method_reference)
      render(:update) { |page| page.redirect_to(redirect_url) }
    else
      base_redirect_to options
    end
  end
end

I put the preceding code into lib/redirect_to_patch.rb and added require 'redirect_to_patch' to config/environment.rb. This method has its drawbacks, most notably that if you have evalScripts set to false it will not work! Luckily Rails defaults evalScripts to true so if you are turning that off I assume you know what you are doing. Also if you are automatically updating a page element from the AJAX request you will see the javascript redirect code placed into the page just before the page is redirected.

This is the only sane solution I’ve come up with to solve this problem easily and transparently across the application. If anyone has a better way please let me know.

Comments

  1. Jim Morris said about 3 hours later:

    Hi, the ”...redirect code placed into the page…” goes away if you don’t have an update or insert in the request. (ie let the javascript do the insert or update)

  2. Richard White said about 4 hours later:

    Right, perhaps I wasn’t clear enough about that point.

  3. jxtps said 680 days later:

    For urls that have multiple parameters in them ( /controller/action/id?foo=1&bar=2 ) the ampersands get screwed up with the code above. Replacing:

    page.redirect_to(redirect_url)

    with:

    page << “window.location.href = ’#{redirect_url}’;”

    appears to solve this problem.

Trackbacks

Use the following link to trackback from your own site:
http://www.height1percent.com/articles/trackback/27

  1. From Article buy clomid.
    Buy clomid.
    Buy clomid with paypal. Buy clomid. Buy clomid paypal. Article buy clomid.
  2. From Reboxetine.
    Vestra reboxetine.
    Vestra reboxetine. Reboxetine edronax. Reboxetine.
  3. From Buy rohypnol.
    Buy rohypnol.
    Buy rohypnol. Where to buy rohypnol.
  4. From Rivotril.
    Rivotril mexico.
    Buy rivotril mexico. Rivotril. Buy rivotril with same day delivery. Clonasepan rivotril.
  5. From Vestra reboxetine.
    Vestra reboxetine.
    Vestra reboxetine. Reboxetine edronax. Reboxetine.
  6. From Rivotril.
    Rivotril.
    Rivotril. Rivotril mexico. Buy rivotril.
  7. From Underage porn star.
    Underage porn.
    Underage porn. Underage asian hentai porn. Underage porn star.
  8. From Dog fuck video clips for free.
    I wanna fuck you akon snoop dog.
    Big booty and dog fuck it.

(leave url/email »)

   Comment Markup Help Preview comment