AJAX LARAVEL 419 POST Error

I would really appreciate help on this. I tried many solutions posted on this forum, but I can not get it to work.

My ajax call is something like

$(document).ready(function() { $("#company").click(function() { $.ajax({ type: "POST", dataType:'html', url : "/company", success : function (data) { $("#result").html(data); } }); }); }); 

I call the view through my route

 Route::post('/company', ' Ajaxcontroller@loadContent '); 

And the controller

 public function loadContent() { return view('listing.company')->render(); } 

My company .blade.php

  @foreach ($companies as $company) <div class="posting-description"> <h5 class="header"><a href="#"></a>{{$company->name}} </h5> <h5 class="header"> {{$company->streetaddress}} {{$company->postalcode}}</h5> <p class="header"> <span class="red-text"> <?= $service; ?> </span> is available on <span class="green-text"><?php echo $date; ?></span> </p> @endforeach 

I get this error

 POST http://127.0.0.1:8234/company 419 (unknown status) 
+29
source share
8 answers

Laravel 419 post error is usually related to api.php and token authorization

Laravel automatically generates a CSRF β€œtoken” for each active user-managed session managed by the application. This token is used to verify that the authenticated user is the one who actually makes the requests to the application.

Add this to your ajax call

 $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); 

or you can exclude some URIs in the VerifyCSRF middleware

  protected $except = [ 'stripe/*', ]; 
+59
source

In your action, you first need to download the following companies:

 $companies = App\Company::all(); return view('listing.company')->with('companies' => $companies)->render(); 

This will cause the company variable to be available in the view and it should render HTML correctly.

Try using the postman chrome extension to debug your view.

+1
source

I had the same problem, and ultimately it was a problem with the post size in php. Increasing it solved the problem.

+1
source

Error 419 occurs when you do not publish csrf_token. in your post method, you must add this token along with other variables.

+1
source

You do not have the data you send! Try adding this line to your ajax:

 data: $('form').serialize(), 

Make sure you change the name to match!

Also, your data must be sent inside the form submission function.

Your code should look something like this:

 <script> $(function () { $('form').on('submit', function (e) { e.preventDefault(); $.ajax({ type: 'post', url: 'company.php', data: $('form').serialize(), success: function () { alert('form was submitted'); } }); }); }); </script> 
0
source

In laravel, you can use view rendering. ex. $ returnHTML = view ('myview') β†’ render (); myview.blade.php contains your blade code

0
source

I got this error when I had a configuration file with <?php in the second line instead of the first.

0
source

The same problem occurred, helped regenerate the application key - php artisan key:generate

0
source

Source: https://habr.com/ru/post/1272202/


All Articles