简体   繁体   English

Laravel中的Ajax 419错误

[英]ajax 419 Error in Laravel

Hello i am having a 419 Error that keeps on showing even when i try switching techniques between blade syntax url, normal javasript url ,jason data format or sending my data with the url .Please Help i also included X-CSRF in the head: 您好,我遇到419错误,即使我尝试在刀片语法url,常规javasript url,jason数据格式或使用url发送我的数据之间切换技术,该错误仍会持续显示。请帮助我在头中还包括X-CSRF:

<meta name="csrf-token" content="{{ csrf_token() }}">

My call: 我的电话:

$.ajaxSetup({
    headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
$.ajax({
  url:'{{url("/HeatMapCoordinates")}}',
  data:{"finalPointArray" :finalPointsMap,
        "tourId":tourId
        },
  dataType: 'html',
  async:true,
  type:'post',
  processData: false,
  contentType: false,
  success:function(response){
    console.log('response');
  },
  error:function(e){
    console.log('error');
  }
});
}

my route: Route::resource('/HeatMapCoordinates','HeatMapCoordinatesController'); 我的路线:Route :: resource('/ HeatMapCoordinates','HeatMapCoordinatesController'); My Controller: 我的控制器:

    public function store(Request $request)
    {

        $this->validate($request, array(
            'finalPointArray' => 'required',
            'tourId' => 'required',
        ));

..... } .....}

You havent included the CSRF token. 您尚未包含CSRF令牌。 The field is called csrf-token not _token and it needst to be contained in "" to be a valid selector. 该字段称为csrf-token而不是_token ,它必须包含在""中才能成为有效的选择器。

$.ajaxSetup({
  headers: { 'X-CSRF-Token' : $('meta[name="csrf-token"]').attr('content') } 
});

When trying to call a controller route in ajax, you do not need to use the blade syntax for it. 尝试在ajax中调用控制器路由时,无需对其使用刀片语法。 You can simply just call the url like this 您可以像这样简单地调用网址

url: '/HeatMapCoordinates'

If you have included a csrf token in your html page, try to include this on your ajax data 如果您在html页面中包含了csrf令牌,请尝试将其包含在ajax数据中

'_token': $('input[name=_token]').val(),

Your code will look like this 您的代码将如下所示

$.ajax({
  url: '/HeatMapCoordinates',
  data:{ 
           "_token": $('input[name=_token]').val(),
           "finalPointArray" :finalPointsMap,
           "tourId":tourId
        },

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

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