Laravel + Vue.js (axios) - CSRF token mismatch Laravel + Vue.js (axios) - CSRF token mismatch vue.js vue.js

Laravel + Vue.js (axios) - CSRF token mismatch


I encountered this issue. The cause was axios headers not being set. Setting them on the axis object within the component also did not resolve the issue. Setting the headers after the object is defined resolved the issue for me.

In your blade view add the following:

<script>window.Laravel = {csrfToken: '{{ csrf_token() }}'}</script>

In your bootstrap.js file, after declaring window.axios = require('axios'); add the following:

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';window.axios.defaults.headers.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken;


Sometimes?! It sounds like a expired token.

When you're working in incognito tabs, you have a fresh token.

Try location.reload() when you're getting 419 error code and everything goes well.


I think the token is expired in your case. You can intercept http status 419 (non standard status defined by Laravel) and reload the page to generate a new CSRF token :

window.axios.interceptors.response.use(    response => response.data,    error => {        if (error.response && 419 === error.response.status) {            window.location.reload()        }        return Promise.reject(error)    })