g3cttg
Last Updated: February 25, 2016
·
3.874K
· michiels
Fb613ae74d247c05eba250f575e2c9b0

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.

Say Thanks
Respond

2 Responses
Add your response

3138

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

over 1 year ago ·
3217
Fb613ae74d247c05eba250f575e2c9b0

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

over 1 year ago ·