Skip to content

Instantly share code, notes, and snippets.

@LouisdeBruijn
Last active December 18, 2019 23:19
Show Gist options
  • Select an option

  • Save LouisdeBruijn/613dafe9dea171161a0591c0b89db2e2 to your computer and use it in GitHub Desktop.

Select an option

Save LouisdeBruijn/613dafe9dea171161a0591c0b89db2e2 to your computer and use it in GitHub Desktop.
CSRF protection for AJAX request via Flask-WTF
var csrf_token = "{{ csrf_token() }}";
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment