Skip to content

Instantly share code, notes, and snippets.

@mapreal19
Created September 8, 2015 09:09
Show Gist options
  • Save mapreal19/338356e2e7a3140c45bb to your computer and use it in GitHub Desktop.
Save mapreal19/338356e2e7a3140c45bb to your computer and use it in GitHub Desktop.
fetch-in-rails
submitComment(comment) {
const csrfToken = $('meta[name=csrf-token]').attr('content'); // Retrieve token from `csrf_meta_tags`
return fetch(API_URL, {
method: 'post',
credentials: 'same-origin', // Needed. See https://github.com/github/fetch#sending-cookies
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-CSRF-Token': csrfToken,
},
body: JSON.stringify({comment}),
}).then(checkStatus).then(parseJSON);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment