PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

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