Skip to content

Instantly share code, notes, and snippets.

Embed
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')
return
oldSync method, model, options
@cmdelatorre

This comment has been minimized.

Copy link
Owner Author

commented Aug 25, 2015

This is based in https://docs.djangoproject.com/en/1.7/ref/contrib/csrf/#ajax (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.