Created
August 25, 2015 14:09
-
-
Save cmdelatorre/8cd3de8b2006abfa48a8 to your computer and use it in GitHub Desktop.
Integrate Backbone with Django: Add CSRF Token to Backbone Ajax calls
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is based in https://docs.djangoproject.com/en/1.7/ref/contrib/csrf/#ajax (using the jQuery cookie plugin)