Rumah >pembangunan bahagian belakang >tutorial php >怎么解决“laravel X-CSRF-TOKEN','Authorization”问题?

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

PHPz
PHPzasal
2016-06-06 20:30:472668semak imbas

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

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn