Skip to content

Instantly share code, notes, and snippets.

@z4r
Created June 21, 2013 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z4r/5832725 to your computer and use it in GitHub Desktop.
Save z4r/5832725 to your computer and use it in GitHub Desktop.
ajax csrf for django
//require jquery.cookie.js
var csrftoken = $.cookie('csrftoken');
function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain: false,
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment