Laravel csrf token gives an error in laravel 5.4

时间:2018-03-25 20:06:59

标签: php laravel laravel-5.4

Guys I used <meta name="csrf-token" content="{{ csrf_token() }}"> in the head section. And I use ajax to submit some data to controller, and that works fine.

But I got one form in the same page to upload a image. When I submit data from that form laravel gives me a TokenMismatchException error in in VerifyCsrfToken.php (line 68). What I'm missing here?

3 个答案:

答案 0 :(得分:0)

尝试把它放在你的表格中

<input type="hidden" name="_token" value="{{ csrf_token() }}">

答案 1 :(得分:0)

在页面范围中将csrf-token添加到ajax请求中:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

答案 2 :(得分:0)

对于您发送的每个请求,如果您使用jQuer ajax,您可以获取令牌的值并将其传递给数据。

data: {_token: $('meta[name="csrf-token"]').attr('content')}