Last active
August 29, 2015 14:18
-
-
Save MechanisM/ea2c67445a93b6e305c0 to your computer and use it in GitHub Desktop.
Django Ajax with CSRF for Polymer
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
<link rel="import" href="{{ STATIC_URL }}vendor/polymer/polymer.html"> | |
<link rel="import" href="{{ STATIC_URL }}vendor/core-ajax/core-ajax.html"> | |
<polymer-element name="django-ajax" extends="core-ajax"> | |
<script> | |
Polymer({ | |
ready: function() { | |
this.super(); | |
this.headers = { | |
"X-CSRFToken": {{ csrf_token }}, | |
"X-Requested-With": "XMLHttpRequest" | |
}; | |
} | |
}); | |
</script> | |
</polymer-element> |
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
<link rel="import" href="/assets/vendor/polymer/polymer.html"> | |
<link rel="import" href="/assets/vendor/core-ajax/core-ajax.html"> | |
<polymer-element name="django-ajax" extends="core-ajax"> | |
<script> | |
Polymer({ | |
getCSRFCookie: function() { | |
b = document.cookie.match('(^|;)\\s*csrftoken\\s*=\\s*([^;]+)'); | |
return b ? b.pop() : ''; | |
}, | |
ready: function() { | |
this.super(); | |
this.headers = { | |
"X-CSRFToken": this.getCSRFCookie(), | |
"X-Requested-With": "XMLHttpRequest" | |
}; | |
} | |
}); | |
</script> | |
</polymer-element> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment