解决Laravel 5.5 header['X-CSRF-TOKEN','Authorization']请求问题

1、解决 "CSRF-TOKEN",需要在主模板里更改:

In header

 name="csrf-token" content="{{ csrf_token() }}" />
2、解决 "X-CSRF-TOKEN",需要在主模板里更改:

In header

 name="csrf-token" content="{{ csrf_token() }}" />

In script(Ajax)

 type="text/javascript">
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
In vue2.5
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let 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');
}

3、如果使用Vue2.5 Axios api Token,则需要如下更改(记得在数据库中设置token值):

In header

In script(Ajax)
 type="text/javascript">
$.ajaxSetup({
    headers: {'Authorization': $('meta[name="api-token"]').attr('content')
    }
});
In Vue 2.5
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
let apiToken = document.head.querySelector('meta[name="api-token"]');
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');
}

你可能感兴趣的:(laravel,Axios,Token,laravel,Vue)