简体   繁体   English

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.我尝试了该论坛中发布的大量解决方案,但无法使其正常工作。

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)

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. Laravel 会为应用程序管理的每个活动用户会话自动生成一个 CSRF “令牌”。 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将此添加到您的 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 = [
        '/route_you_want_to_ignore',
        '/route_group/*
    ];

419 error happens when you don`t post csrf_token.当您不发布 csrf_token 时会发生 419 错误。 in your post method you must add this token along other variables.在您的 post 方法中,您必须将此标记与其他变量一起添加。

有同样的问题,重新生成应用程序密钥有帮助 - php artisan key:generate

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>

I had the same issue, and it ended up being a problem with the php max post size.我遇到了同样的问题,最终成为 php max post size 的问题。 Increasing it solved the problem.增加它解决了这个问题。

当我在第二行而不是第一行有一个带有<?php的配置文件时,我收到了这个错误。

当活动用户会话的 CSRF“令牌”过期时,即使在 ajax 请求中指定了令牌,您也可能会收到该错误。

In laravel you can use view render.在 laravel 中,您可以使用视图渲染。 ex.前任。 $returnHTML = view('myview')->render(); $returnHTML = view('myview')->render(); myview.blade.php contains your blade code myview.blade.php 包含您的刀片代码

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 扩展来调试您的视图。

Step 1: Put the csrf meta tag in head第 1 步:将 csrf 元标记放入 head

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Document</title>
</head>
<body>

Step 2: Use this ajax format第 2 步:使用此 ajax 格式

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
      $("#frm").submit(function(e){
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
            });
        $.ajax({
                url:"{{ url('form_submit') }}",
                data:$('frm').serialize(),
                type:'post',
                success: function(result){
                    console.log(result);
                }
        });
      });
    });
</script>

for me this happens now and then when running a unit test对我来说,这在运行单元测试时不时发生

php artisan config:clear

helped me帮助过我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM