Search code examples
laravelcsrf

Laravel CSRF token not found


    <meta charset="UTF-8" />
    <meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0,  maximum-scale=1.0, minimum-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    <link rel="stylesheet" href="{{asset('css/app.css')}}" />
    <link rel="stylesheet" href="{{asset('css/main.css')}}" />

<body>
<script src="{{asset('js/app.js')}}"></script>
</body>

but I get CSRF token not found message in Console


Solution

  • I think you have to manually instruct JS to add token to all request headers, If you're not using this default js file:

    resources/assets/js/bootstrap.js

    If that's the case then you can add this JQuery code to manually do that:

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