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)
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/*
];
419 error happens when you don`t post csrf_token. in your post method you must add this token along other variables.
Had the same problem, regenerating application key helped - php artisan key:generate
I had the same issue, and it ended up being a problem with the php max post size. Increasing it solved the problem.
You don't have any data that you're submitting! Try adding this line to your ajax:
data: $('form').serialize(),
Make sure you change the name to match!
Also your data should be submitted inside of a form submit function.
Your code should look something like this:
<script>_x000D_
$(function () {_x000D_
$('form').on('submit', function (e) {_x000D_
e.preventDefault();_x000D_
$.ajax({_x000D_
type: 'post',_x000D_
url: 'company.php',_x000D_
data: $('form').serialize(),_x000D_
success: function () {_x000D_
alert('form was submitted');_x000D_
}_x000D_
});_x000D_
});_x000D_
});_x000D_
</script>
_x000D_
I received this error when I had a config file with <?php
on the second line instead of the first.
You may also get that error when CSRF "token" for the active user session is out of date, even if the token was specified in ajax request.
In laravel you can use view render. ex. $returnHTML = view('myview')->render(); myview.blade.php contains your blade code
In your action you need first to load companies like so :
$companies = App\Company::all();
return view('listing.company')->with('companies' => $companies)->render();
This will make the companies variable available in the view, and it should render the HTML correctly.
Try to use postman chrome extension to debug your view.
Source: Stackoverflow.com