bootstrap.js 如下
let token = document.head.querySelector('meta[name="csrf-token"]');
let apiToken = document.head.querySelector('meta[name="apiToken"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
window.axios.defaults.headers.common['Authorization'] = apiToken.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
app.blade.php如下
<meta name="csrf-token" content="{ csrf_token() }">
<meta name="apiToken" content="{ Auth::check() ? 'Bearer '.Auth::user()->api_token : 'Bearer ' }">