Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 213
Cowgirl Avatar asked Sep 28 '17 15:09

Cowgirl


People also ask

What is 419 error in Laravel?

This is a very common issue when you got the 419 page expired issue in the Laravel application. This happens due to inactivity on the page for a long time. Laravel handles the form request with a CSRF (Cross-Site Request Forgery) token. For every form submit, Laravel generates a new token.

What is error code 419?

What does 419 HTTP Status Code Mean? The HTTP Status Code 419 indicates that a session has expired while processing a post request.

How do I fix 419 page expired?

The Session Expired or 419 Page Expired error message in Laravel comes up because somewhere your csrf token verification fails which means the App\Http\Middleware\VerifyCsrfToken::class middleware is already turned on. In the form the @csrf blade directive is already added, which should be fine as well.


2 Answers

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 = [         '/route_you_want_to_ignore',         '/route_group/*     ]; 
like image 173
Dhiraj Avatar answered Oct 14 '22 19:10

Dhiraj


419 error happens when you don`t post csrf_token. in your post method you must add this token along other variables.

like image 23
Adnan Rasheed Avatar answered Oct 14 '22 18:10

Adnan Rasheed