Skip to content

Instantly share code, notes, and snippets.

@rvause
Created December 11, 2013 13:21
Show Gist options
  • Select an option

  • Save rvause/7910324 to your computer and use it in GitHub Desktop.

Select an option

Save rvause/7910324 to your computer and use it in GitHub Desktop.
getCookie = (name) ->
cookieValue = null
if document.cookie && document.cookie != ''
cookies = document.cookie.split ';'
for cookie in cookies
cookie = jQuery.trim cookie
if cookie.substring(0, name.length + 1) == "#{ name }="
cookieValue = decodeURIComponent cookie.substring(name.length + 1)
break
return cookieValue
csrfSafeMethod = (method) ->
/^(GET|HEAD|OPTIONS|TRACE)$/.test method
$.ajaxSetup
crossDomain: false
beforeSend: (xhr, settings) ->
if not csrfSafeMethod settings.type
xhr.setRequestHeader 'X-CSRFToken', getCookie('csrftoken')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment