R
R
Ruslan Absalyamov2018-03-06 20:21:41
Laravel
Ruslan Absalyamov, 2018-03-06 20:21:41

How to get rid of CSRF token not found error in laravel?

I have a backend project using laravel and a react frontend and I have such a problem when I did this command php artisan preset react. I looked in resources/assets/js/bootstrap.js and there axios should connect

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

But for some reason there is an error, who can tell how to fix it? put the csrf token into the template itself or what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maddog670, 2018-03-06
@rusline18

one.

<meta name="csrf-token" content="{{ csrf_token() }}">

2.
<script>
window.Laravel = <?php echo json_encode([
    'csrfToken' => csrf_token(),
]); ?>
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question