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

Question By
Solved

Help with Javacript to Laravel

Solved
bernardcpa

Oct 20th, 2021 01:49 PM

I am using an API and they have given me a Javascript example of how to get a token. However, I need help with getting that token back to Laravel. I think I need a POST function of some sort but I do not know where or how. Please help. Thanks.

<script></script>
<script>
  // Part 2. Initialize & configure the client library
  document.addEventListener("DOMContentLoaded", function() {
    var tellerConnect = TellerConnect.setup({
      applicationId: "app_nadv4thdhoronhtebc000",
      environment: "sandbox",
      onInit: function() {
        console.log("Teller Connect has initialized");
      },
      // Part 3. Handle a successful enrollment's accessToken
      onSuccess: function(enrollment) {
        console.log("User enrolled successfully", enrollment.accessToken);
        

      },
      onExit: function() {
        console.log("User closed Teller Connect");
      }
    });

    // Part 4. Hook user actions to start Teller Connect
    var el = document.getElementById("teller-connect");
    el.addEventListener("click", function() {
      tellerConnect.open();
    });
  });
</script>
bobbyiliev

Oct 21st, 2021 10:40 AM

Best Answer

Hello,

Yes indeed you are right, it will be more or less a standard JS AJAX request. You could use fetch or a library like Axios depending on your personal preferences.

fetch('/your-larave-route', {
    method: 'POST', 
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'url': '/your-larave-route',
        "X-CSRF-Token": document.querySelector('input[name=_token]').value
    },
})

Also remember to include the @csrf helper in your blade view so that the X-CSRF-Token could have a correct value.

Let me know how it goes!

Best,

Bobby

Report
1
bernardcpa

Oct 21st, 2021 10:47 AM

Thank you!

Report
1