0

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',
        ));

..... }

2 Answers 2

1

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') } 
});
Sign up to request clarification or add additional context in comments.

4 Comments

Hello @mikeMiller Thank you for your suggestion But the console showed me this error when i tried it. Any idea what caused it? Error: app.js:1 POST localhost/switch_to_vr/public/HeatMapCoordinates 422 (Unprocessable Entity)
422 status code related Laravel validation. Are you sure, Do you post required data and right format ?
@SandyAlAkhras that is as @webdevtr says a validation error so you probably have null value inside one of your JS variables; finalPointsMap or tourId. If you do console.log( finalPointsMap , tourId ) it will show you which are null. Alternatively inspect the response body by doing console.log(e) to see the validation messages from your API
Worked turns out i had a syntax error in the controller which caused the the 422 (Unprocessable Entity) error. Thanks for the help!
0

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
        },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.