我想指定将应用于每个请求的配置默认值:
// app/javascript/setupCSRFToken.js
import axios from 'axios'
export default function() {
const csrfToken = document.querySelector("meta[name=csrf-token]").content
axios.defaults.headers.common['X-CSRF-Token'] = csrfToken
}
// app/javascript/packs/application.js
import setupCSRFToken from '../setupCSRFToken'
setupCSRFToken()
但它似乎并没有被应用。在React组件(导入axios)中调用它只会返回undefined
:
componentWillMount() {
console.log(axios.defaults.headers.common['X-CSRF-TOKEN'])
}