Skip to content

Instantly share code, notes, and snippets.

@milk1000cc
Created January 26, 2019 05:59
Show Gist options
  • Save milk1000cc/063032c71521061458a2adf714cec616 to your computer and use it in GitHub Desktop.
Save milk1000cc/063032c71521061458a2adf714cec616 to your computer and use it in GitHub Desktop.
axios + rails csrf token + common error handling
import axios from 'axios'
import { csrfToken } from 'rails-ujs'
const myAxios = axios.create()
myAxios.defaults.headers.common['X-CSRF-Token'] = csrfToken()
myAxios.interceptors.response.use(res => res, error => {
const msg = "エラーが発生しました。\n\nしばらく時間をおいてから、もう一度お試しください。\n" +
"ご不便をおかけしてしまい、大変申し訳ございません。"
alert(msg)
return Promise.reject(error)
})
export default myAxios
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment