php Ajax LARAVEL 419 POST 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46472812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax LARAVEL 419 POST error
提问by Cowgirl
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
我的 ajax 调用类似于
$(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
我的 company.blade.php 是
@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)
回答by Dhiraj
Laravel 419 post error is usually related with api.php and token authorization
Laravel 419 post 错误通常与 api.php 和 token 授权有关
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.
Laravel 会为应用程序管理的每个活动用户会话自动生成一个 CSRF “令牌”。此令牌用于验证经过身份验证的用户是否是实际向应用程序发出请求的用户。
Add this to your ajax call
将此添加到您的 ajax 调用中
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
or you can exclude some URIs in VerifyCSRF token middleware
或者您可以在 VerifyCSRF 令牌中间件中排除一些 URI
protected $except = [
'stripe/*',
];
回答by Adnan Rasheed
419 error happens when you don`t post csrf_token. in your post method you must add this token along other variables.
当您不发布 csrf_token 时会发生 419 错误。在您的 post 方法中,您必须将此标记与其他变量一起添加。
回答by EXayer
Had the same problem, regenerating application key helped - php artisan key:generate
有同样的问题,重新生成应用程序密钥有帮助 - php artisan key:generate
回答by Lulceltech
You don't have any data that you're submitting! Try adding this line to your ajax:
您没有要提交的任何数据!尝试将此行添加到您的 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>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'company.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
});
});
</script>
回答by Francisco Isidori
I had the same issue, and it ended up being a problem with the php max post size. Increasing it solved the problem.
我遇到了同样的问题,结果是 php max post size 的问题。增加它解决了问题。
回答by eli
I received this error when I had a config file with <?php
on the second line instead of the first.
当我<?php
在第二行而不是第一行有一个配置文件时,我收到了这个错误。
回答by Александр Волошиновский
In laravel you can use view render. ex. $returnHTML = view('myview')->render(); myview.blade.php contains your blade code
在 Laravel 中,您可以使用视图渲染。前任。$returnHTML = view('myview')->render(); myview.blade.php 包含您的刀片代码
回答by teeyo
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.
这将使公司变量在视图中可用,并且应该正确呈现 HTML。
Try to use postman chrome extension to debug your view.
尝试使用 postman chrome 扩展来调试您的视图。
回答by eldorjon
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.
当活动用户会话的 CSRF“令牌”过期时,您也可能会收到该错误,即使令牌是在 ajax 请求中指定的。