Created
October 23, 2019 09:40
-
-
Save virolea/5edba91068ce7d6a824b1e5e2dc7ef7c to your computer and use it in GitHub Desktop.
Configure axios to automatically set the Rails CSRF token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
const tokenEl = document.getElementsByName('csrf-token')[0] | |
if (tokenEl) { | |
const token = tokenEl.getAttribute('content') | |
axios.defaults.headers.common['X-CSRF-Token'] = token | |
} | |
axios.defaults.headers.common['Accept'] = 'application/json' | |
axios.defaults.headers.post['Content-Type'] = 'application/json' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment