How to include csrf token in an external js file in Laravel

0 votes

Initially, I wrote all the JavaScript code right in my HTML page using the <script> tag.

A post call in jQuery in the JavaScript was something like this.

$.post('store',{'_token':'{{csrf_token()}}'}, function(data){ /*a bunch of code*/ });

The code worked fine. But, later I put all my script into an external js file. And the code is not working anymore.

I have the problem with the {{csrf_token()}}, the error being

TokenMismatchException in compiled.php

What should I do if I want to external js files with Laravel?

Jun 11, 2020 in Java-Script by kartik
• 37,510 points
3,403 views

1 answer to this question.

0 votes

Hello @kartik,

To resolve this error you can follow this steps:

  1. Add <meta> tag with the token to the blade layout:
<meta name="_token" content="{{ csrf_token() }}">
  1. Setup ajax requests:
$(function() {
  $.ajaxSetup({
    headers: {
      'X-CSRF-Token': $('meta[name="_token"]').attr('content')
    }
  });
});

Now you can use $.post() without providing _token each time.

Thank you!!

answered Jun 11, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

+1 vote
1 answer

How to Check synchronously if file/directory exists in Node.js?

Hello, You can use fs.existsSync(): const fs = require("fs"); // ...READ MORE

answered May 28, 2020 in Java-Script by Niroj
• 82,880 points
1,617 views
0 votes
0 answers

How to access PHP session variables from jQuery function in a .js file?

How to access PHP session variables from ...READ MORE

Jul 7, 2020 in Java-Script by kartik
• 37,510 points
977 views
0 votes
1 answer

How can we create an HTTPS server in Node.js?

Hii, The Express API doc spells this out pretty clearly. I ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,880 points
532 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,880 points
755 views
0 votes
1 answer

Error: Use of const in strict mode

Hello, If this is happening in nodejs, it ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,880 points
3,245 views
0 votes
1 answer

How do I install the babel-polyfill library?

Hello @kartik, If your package.json looks something like ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,880 points
1,526 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,880 points
4,096 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
2,913 views
–1 vote
1 answer

How to read an external local JSON file in JavaScript?

Hello @kartik, For reading the external Local JSON ...READ MORE

answered Sep 3, 2020 in Java-Script by Niroj
• 82,880 points
182,465 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,880 points
12,234 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP