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

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

PHPz
PHPzOriginal
2016-06-06 20:30:472717Durchsuche

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

怎么解决“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: {
        &#39;X-CSRF-TOKEN&#39;: $(&#39;meta[name="csrf-token"]&#39;).attr(&#39;content&#39;)
    }
});
</script>

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;);
}

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>

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;);
}

推荐教程:《laravel入门

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn