Created
June 21, 2013 17:09
-
-
Save z4r/5832725 to your computer and use it in GitHub Desktop.
ajax csrf for django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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