Skip to content

Instantly share code, notes, and snippets.

@jacobh
Created May 25, 2012 04:43
Show Gist options
  • Save jacobh/2785812 to your computer and use it in GitHub Desktop.
Save jacobh/2785812 to your computer and use it in GitHub Desktop.
Django ajax csrf coffeescript
$.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'))
}
)
@michaelficarra
Copy link

No problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment