Created
May 25, 2012 04:43
-
-
Save jacobh/2785812 to your computer and use it in GitHub Desktop.
Django ajax csrf coffeescript
This file contains 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
$.ajaxSetup( | |
{ | |
beforeSend: (xhr, settings) -> | |
getCookie = (name) -> | |
cookieValue = null | |
if (document.cookie and document.cookie) isnt '' | |
cookies = document.cookie.split(';') | |
for cookie in cookies | |
console.log cookie | |
if cookie.substring(0, name.length+1) is name+'=' | |
cookieValue = decodeURIComponent( | |
cookie.substring(name.length+1) | |
) | |
break | |
return cookieValue | |
if not (/^http:.*/.test(settings.url) or /^https:.*/.test(settings.url)) | |
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')) | |
} | |
) |
I just did a straight conversion of some js that was on SO.
…On May 25, 2012 1:43 PM, "Michael Ficarra" < ***@***.***> wrote:
This is officially the least idomatic CoffeeScript code I've ever seen. I
weep for your colleagues.
``` coffee
getCookie = (name) ->
if document.cookie isnt ''
for cookie in document.cookie.split ';'
if name is (cookie.split '=')[0]
return decodeURIComponent cookie[(1 + name.length)...]
null
$.ajaxSetup
beforeSend: (xhr, settings) ->
unless (/^http:.*/.test settings.url) or (/^https:.*/.test settings.url)
xhr.setRequestHeader "X-CSRFToken", getCookie 'csrftoken'
```
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2785812
Ah cool. Thanks for the info.
…On May 25, 2012 2:04 PM, "Michael Ficarra" < ***@***.***> wrote:
## http://js2coffee.org
Reply to this email directly or view it on GitHub:
https://gist.github.com/2785812
No problem.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is officially the least idomatic CoffeeScript code I've ever seen. I weep for your colleagues.