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?

Oct 21, 2020 in Laravel by kartik
• 37,530 points
379 views

1 answer to this question.

0 votes

Hello @kartik,

  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 Oct 21, 2020 by Niroj
• 82,820 points

Related Questions In Laravel

0 votes
1 answer

How to validate an input field if value is not null in Laravel?

Hello @kartik, try using nullable as a rule 'password' ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,820 points
5,905 views
0 votes
2 answers

How to delete file from public folder in laravel?

I just want to add @niroj answer  Use ...READ MORE

answered Nov 5, 2020 in Laravel by Helloworld
• 140 points
4,091 views
0 votes
1 answer

How to invalidate all tokens for an user in laravel passport?

Hello @kartik, One of the methods it provides ...READ MORE

answered Oct 23, 2020 in Laravel by Niroj
• 82,820 points
1,368 views
0 votes
1 answer

How to pass URL param in Laravel?

Hello, First you have to go to routes ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
3,412 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
9,830 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
1,094 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
883 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
10,966 views
0 votes
1 answer

How to include a sub-view in Blade templates in Laravel?

Hello @kartik, You can use the blade template ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,820 points
7,604 views
0 votes
1 answer

How to populating a database in a Laravel migration file?

Hello @kartik, Don't put the DB::insert() inside of ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,820 points
1,066 views