Skip to content

Instantly share code, notes, and snippets.

@jhrr
Created February 16, 2014 13:05
Show Gist options
  • Save jhrr/9033927 to your computer and use it in GitHub Desktop.
Save jhrr/9033927 to your computer and use it in GitHub Desktop.
Embed the CSRF token in a meta tag in the HTML header, and to modify Sync to take the token from the DOM and add it to the AJAX request's HTTP headers. From: http://ozkatz.github.io/backbonejs-with-django-15.html
var oldSync = Backbone.sync;
Backbone.sync = function(method, model, options){
options.beforeSend = function(xhr){
xhr.setRequestHeader('X-CSRFToken', CSRF_TOKEN);
};
return oldSync(method, model, options);
};
<!-- inside our page's <head> tag -->
<meta name="csrf-token" content="{{csrf_token}}">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment