Chris Mkp Chris Mkp - 3 years ago 362
jQuery Question

Ajax LARAVEL 419 POST error

I would really appreciate some help on this.
I tried tons of solutions as posted in this forum, but I cannot 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 am calling the view through my route

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


And controller

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


My company.blade.php is

@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 am getting this error

POST http://127.0.0.1:8234/company 419 (unknown status)

Answer Source

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

Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making 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 VerifyCSRF token middleware

 protected $except = [
        'stripe/*',
    ];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download