Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Integrate Backbone with Django: Add CSRF Token to Backbone Ajax calls
oldSync = Backbone.sync
Backbone.sync = (method, model, options) ->
csrfSafeMethod = (method) ->
# these HTTP methods do not require CSRF protection
/^(GET|HEAD|OPTIONS|TRACE)$/.test method
options.beforeSend = (xhr, settings) ->
if !csrfSafeMethod(settings.type) and !@crossDomain
xhr.setRequestHeader 'X-CSRFToken', $.cookie('csrftoken')
oldSync method, model, options

This comment has been minimized.

Copy link
Owner Author

commented Aug 25, 2015

This is based in (using the jQuery cookie plugin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.