Skip to content

Instantly share code, notes, and snippets.

@jaddison
Created February 29, 2012 15:51
Show Gist options
  • Save jaddison/1941916 to your computer and use it in GitHub Desktop.
Save jaddison/1941916 to your computer and use it in GitHub Desktop.
django csrf token in jquery
$(document).ready(function() {
// Disable caching of AJAX responses
$.ajaxSetup ({cache: false});
$('html').ajaxSend(function(event, xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment