Laravel Get CSRF Token with Vanilla Javascript

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

If you need to do an AJAX POST request in Laravel and you check out the documentation, you're likely to see the solution for retrieving the CSRF token in jQuery like so:

var csrf = $('meta[name="csrf-token"]').attr('content');

But... What if you just want to retrieve it in Vanilla Javascript? Simple enough, you can use the code below:

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

Hope that helps you out.

BETA Snippet explanation automatically generated by OpenAI:

No explanation generated yet.

Snippet By Dev Dojo

·

Created June 12th, 2021

·

Report Snippet