Ajax Laravel 419 POST Error

Problem

I would really be thankful if someone could help me with this. I am trying to make an Ajax call but I am getting 419 POST error.

My ajax call is something like

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

I am calling the blade template through my Route:

Route::post('/users', 'Ajaxcontroller@loadUsers');
And Controller

public function loadContent()
    {
        return view('listing.users')->render();
    }
My company.blade.php is

    @foreach ($users as $user)
            <div class="posting-description">
            <h5 class="header"><a href="#"></a>{{$user->name}}
            </h5>
            <h5 class="header"> {{$user->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/user 419 (unknown status)

Solution

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

Make sure you pass the CSRF token with every AJAX request. That’s a token that Laravel automatically creates for each logged in user and it is used to verify you as an authenticated user.

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/*',
    ];


from Linux Hint https://ift.tt/3fVhsRl

Post a Comment

0 Comments