Last active
January 20, 2016 11:51
-
-
Save RauliL/f327f2f40fddf4716fb5 to your computer and use it in GitHub Desktop.
Django Ajax CSRF setup.
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
(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