Skip to content

Instantly share code, notes, and snippets.

@RauliL
Last active January 20, 2016 11:51
Show Gist options
  • Save RauliL/f327f2f40fddf4716fb5 to your computer and use it in GitHub Desktop.
Save RauliL/f327f2f40fddf4716fb5 to your computer and use it in GitHub Desktop.
Django Ajax CSRF setup.
(function() {
var matches = /(;\s*|^)csrftoken=([^;]+)/.exec(document.cookie || "");
if (matches) {
var token = decodeURIComponent(matches[2]);
$.ajaxSetup({
beforeSend: function(xhr, settings) {
// These HTTP methods do not require CSRF protection.
if (!/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", token);
}
}
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment