使用Laravel 5.1和VueJS的TokenMismatchException

Rog*_*asy 1 laravel vue.js laravel-5.1

我正在尝试使用VueJS发出POST请求.但是,我无法通过TokenMismatchException.我在主Blade模板中有这个元标记:

<meta name="token" id="token" content="{!! csrf_token() !!}">
Run Code Online (Sandbox Code Playgroud)

这是我的VueJS文件的顶部:

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');
Run Code Online (Sandbox Code Playgroud)

这是我的VueJS方法中调用POST的行:

this.$http.post('ads/create/store', this.content);
Run Code Online (Sandbox Code Playgroud)

我已经尝试了太长时间才能接受令牌.任何人都可以帮忙吗?

Dou*_*sar 5

您应该在元标记和JS getAttribute调用中使用'content'属性:

HTML:

<meta id="token" name="token" content="{{ csrf_token() }}">
Run Code Online (Sandbox Code Playgroud)

JS:

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');
Run Code Online (Sandbox Code Playgroud)