Skip to content

Instantly share code, notes, and snippets.

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