Skip to content

Instantly share code, notes, and snippets.

@onyxrev
Last active December 19, 2015 16:29
Show Gist options
  • Save onyxrev/5984572 to your computer and use it in GitHub Desktop.
Save onyxrev/5984572 to your computer and use it in GitHub Desktop.
Code snippet for AJAX I/O niceties like following redirects and passing callback URLs.
(function(){
jQuery.ajaxSetup({
/* this ugly shit is to ensure that Rails gets its happy little CSRF tokens */
'beforeSend': function(xhr) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
// since the anchor bit of the URL isn't sent by the
// browser, it's not available for the rails app in
// request.referrer. since we use the anchor bit of the
// URL quite a lot in our app for client-side routes we
// will send the full URL to rails with this special
// header so we can do fancy things like send back a
// redirect URL upon successful logins to return the user
// to the page they left when they were prompted to login
xhr.setRequestHeader("X-Referrer-Url", window.location.href);
},
complete: (jQuery.ajax.onComplete = function(xhr) {
// allow the backend to specify a URL to visit when the
// action is completed (like ajax login)
var callback_url = xhr.getResponseHeader("X-Callback-Url");
if (!!callback_url) callback_url = "&callback_url=" + encodeURIComponent(callback_url);
// try to handle redirects transparently
var hash_location = xhr.getResponseHeader("X-Hash-Location");
var location = xhr.getResponseHeader("Location");
if ([301, 302].indexOf(xhr.status) !== -1){
// just change the hash if the URL is just an anchor
// fragment
if (hash_location) return window.location.hash = hash_location + callback_url;
if (location) window.location.href = location;
}
})
});
})();

Little jQuery hook that extends AJAX IO for Rails applications. It adds the following:

On Send:

  1. scrapes the page for the csrf-token meta tag to transparently send those params down with each request in the X-CSRF-Token header.

  2. adds X-Referrer-Url which includes the anchor/hash portion of the URL that standard referrer misses

On Receive:

  1. looks for an X-Callback-Url header in the response and, if found, will add that as a param (after the url anchor - this is oriented towards applications with client-managed routes). this is useful if you are, for example, rendering a login page that logs in via ajax and need to communicate the post-login client-side route to the client.

  2. if the response is a 301 or 302, looks for an X-Hash-Location header in the response and, if found, will change the anchor/hash portion of the existing route to the new location. basically like the Location header of a standard 30x redirect only for client-managed routes.

  3. if the response is a 301 or 302, looks for the Location header in the response and, if found, will follow the redirect like a non-ajax 301 or 302 would

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment