首頁 >後端開發 >php教程 >怎么解决“laravel X-CSRF-TOKEN','Authorization”问题?

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

PHPz
PHPz原創
2016-06-06 20:30:472666瀏覽

怎么解决“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入门

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn