怎么解决“laravel X-CSRF-TOKEN','Authorization”问题?
程序员文章站
2022-05-29 16:10:05
...
怎么解决“laravel X-CSRF-TOKEN','Authorization”请求问题?
1、解决 "CSRF-TOKEN",需要在主模板里更改:
In header
<meta name="csrf-token" content="{{ csrf_token() }}" />
2、解决 "X-CSRF-TOKEN",需要在主模板里更改:
In header
<meta name="csrf-token" content="{{ csrf_token() }}" />
In script(Ajax)
<script type="text/javascript"> $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); </script>
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
<meta name="api-token" content="{{ Auth::check() ? 'Bearer '.Auth::user()->api_token : 'Bearer ' }}"> In script(Ajax) <script type="text/javascript"> $.ajaxSetup({ headers: {'Authorization': $('meta[name="api-token"]').attr('content') } }); </script>
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入门》
下一篇: 新手关于$coon的连接问题