Last Updated: February 25, 2016
·
4.751K
· michiels

Follow redirects with jQuery PJAX

jQuery PJAX follows any redirect just like you're used to. The only difference is that it updates the address bar with the URL of the initial request.

So for instance, when I link to the frontpage of http://volgendboek.nl, which redirects to http://volgendboek.nl/fantasy. The address bar does not reflect the final redirected URL.

To make this work, you can add pass a X-PJAX-URL header to the final request.

Sample in Rails:

class HomepageController < ApplicationController

  def index
    if request.xhr?
      if request.url # Check if we are redirected
        response.headers['X-PJAX-URL'] = request.url
      end
      render :layout => false
    end
  end

end

This will make sure the browser's address bar gets updated to the final URL after the redirect instead of the initial link.

2 Responses
Add your response

hi;]
thanks for quick article! one question: did U have typo in "reponse"? Should't be "reSponse"?

over 1 year ago ·

@hash4di Ah! It should be "response" yes. I am going to fix that in the protip.

over 1 year ago ·