简体   繁体   中英

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:

<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'); My Controller:

    public function store(Request $request)
    {

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

..... }

You havent included the CSRF token. The field is called csrf-token not _token and it needst to be contained in "" to be a valid selector.

$.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. 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

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

Your code will look like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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