Home > Backend Development > PHP Tutorial > 怎么解决“laravel X-CSRF-TOKEN','Authorization”问题?

怎么解决“laravel X-CSRF-TOKEN','Authorization”问题?

PHPz
Release: 2020-06-28 10:12:54
Original
2672 people have browsed it

怎么解决“laravel X-CSRF-TOKEN','Authorization”问题?

怎么解决“laravel X-CSRF-TOKEN','Authorization”请求问题?

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

In header

<meta name="csrf-token" content="{{ csrf_token() }}" />
Copy after login
Copy after login

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

In header

<meta name="csrf-token" content="{{ csrf_token() }}" />
Copy after login
Copy after login

In script(Ajax)

<script type="text/javascript">
$.ajaxSetup({
    headers: {
        &#39;X-CSRF-TOKEN&#39;: $(&#39;meta[name="csrf-token"]&#39;).attr(&#39;content&#39;)
    }
});
</script>
Copy after login

In vue2.5

window.axios = require(&#39;axios&#39;);
window.axios.defaults.headers.common[&#39;X-Requested-With&#39;] = &#39;XMLHttpRequest&#39;;
let token = document.head.querySelector(&#39;meta[name="csrf-token"]&#39;);
if (token) {
    window.axios.defaults.headers.common[&#39;X-CSRF-TOKEN&#39;] = token.content;
} else {
    console.error(&#39;CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token&#39;);
}
Copy after login

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

In header

<meta name="api-token" content="{{ Auth::check() ? &#39;Bearer &#39;.Auth::user()->api_token : &#39;Bearer &#39; }}">
In script(Ajax)
<script type="text/javascript">
$.ajaxSetup({
    headers: {&#39;Authorization&#39;: $(&#39;meta[name="api-token"]&#39;).attr(&#39;content&#39;)
    }
});
</script>
Copy after login

In Vue 2.5

window.axios = require(&#39;axios&#39;);
window.axios.defaults.headers.common[&#39;X-Requested-With&#39;] = &#39;XMLHttpRequest&#39;;
let token = document.head.querySelector(&#39;meta[name="csrf-token"]&#39;);
let apiToken = document.head.querySelector(&#39;meta[name="api-token"]&#39;);
if (token) {
    window.axios.defaults.headers.common[&#39;X-CSRF-TOKEN&#39;] = token.content;
    window.axios.defaults.headers.common[&#39;Authorization&#39;] = apiToken.content;
} else {
    console.error(&#39;CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token&#39;);
}
Copy after login

推荐教程:《laravel入门

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template