Ajax LARAVEL 419 POST错误 [英] Ajax LARAVEL 419 POST error

查看:84
本文介绍了Ajax LARAVEL 419 POST错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此方面,我将非常感谢您的帮助. 我尝试了本论坛中发布的大量解决方案,但无法使其正常工作.

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调用类似

$(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发布错误通常与api.php和令牌授权有关

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 = [
        'stripe/*',
    ];

这篇关于Ajax LARAVEL 419 POST错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆