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 20 in Laravel by kartik
• 35,300 points
48 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 20 by Niroj
• 76,540 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 in Laravel by Niroj
• 76,540 points
433 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 in Laravel by Helloworld
• 140 points
149 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 in Laravel by Niroj
• 76,540 points
61 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 in Laravel by Niroj
• 76,540 points
1,536 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 in Laravel by Niroj
• 76,540 points
3,441 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18 in Laravel by Niroj
• 76,540 points
411 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23 in Laravel by Niroj
• 76,540 points
281 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 in Laravel by Niroj
• 76,540 points
4,142 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 in Laravel by Niroj
• 76,540 points
2,768 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 in Laravel by Niroj
• 76,540 points
120 views