Skip to content

Instantly share code, notes, and snippets.

@mbrochh
Created August 13, 2012 09:31
Show Gist options
  • Select an option

  • Save mbrochh/3338679 to your computer and use it in GitHub Desktop.

Select an option

Save mbrochh/3338679 to your computer and use it in GitHub Desktop.
Get CSRF Token from cookie for AJAX calls against Django views
function getCSRFToken() {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, 10) == ('csrftoken' + '=')) {
cookieValue = decodeURIComponent(cookie.substring(10));
break;
}
}
}
return cookieValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment