Skip to content

Instantly share code, notes, and snippets.

@Tmeister
Created October 30, 2019 16:59
Show Gist options
  • Save Tmeister/6baefe07599ab23c84e1a2322c013032 to your computer and use it in GitHub Desktop.
Save Tmeister/6baefe07599ab23c84e1a2322c013032 to your computer and use it in GitHub Desktop.
Add CSRF to axios
/**
* 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