Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save localhostdotdev/3c3f9291c0703c9bf9d436da9679d755 to your computer and use it in GitHub Desktop.
Save localhostdotdev/3c3f9291c0703c9bf9d436da9679d755 to your computer and use it in GitHub Desktop.
post fetch rails json with csrf token
post = (url, body) ->
token = document.querySelector('meta[name="csrf-token"]').attributes.content.value
fetch("#{url}.json",
method: "POST"
mode: "cors"
cache: "no-cache"
credentials: "same-origin"
headers:
"Content-Type": "application/json"
"X-CSRF-Token": token
redirect: "follow"
referrer: "no-referrer"
body: body
).then (response) -> response.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment