Created
October 30, 2019 16:59
-
-
Save Tmeister/6baefe07599ab23c84e1a2322c013032 to your computer and use it in GitHub Desktop.
Add CSRF to axios
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
/** | |
* Next we will register the CSRF Token as a common header with Axios so that | |
* all outgoing HTTP requests automatically have it attached. This is just | |
* a simple convenience so we don't have to attach every token manually. | |
*/ | |
const token = document.head.querySelector( 'meta[name="csrf-token"]' ); | |
if ( token ) { | |
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; | |
} else { | |
console.error( 'CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment