Ajax LARAVEL 419 POST 错误 [英] 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.
我的ajax调用类似于
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');
和控制器
public function loadContent()
{
return view('listing.company')->render();
}
我的 company.blade.php 是
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
我收到此错误
POST http://127.0.0.1:8234/company 419 (unknown status)
推荐答案
Laravel 419 post错误通常与api.php和token授权有关
Laravel 419 post error is usually related with api.php and token authorization
Laravel 会为应用程序管理的每个活动用户会话自动生成一个 CSRF令牌".此令牌用于验证经过身份验证的用户是实际向应用程序发出请求的用户.
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.
将此添加到您的ajax调用
Add this to your ajax call
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
或者你可以在VerifyCSRF令牌中间件中排除一些URI
or you can exclude some URIs in VerifyCSRF token middleware
protected $except = [
'/route_you_want_to_ignore',
'/route_group/*
];
这篇关于Ajax LARAVEL 419 POST 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!