0

I have a problem with the following code in Laravel. I need to send some variables to my controller, do something with them and return three variables back. I made have made the ajax call, the route and the controller but the ajax call fail. As an error code I receive this.

View

function gg() {
    var slider_value  = document.getElementById('paradnyi').value;
    var checkbox_value  = document.getElementById('check_box').value;
    var dto = {slider_value : slider_value, checkbox_value : checkbox_value};
    $.ajax({
        url : "/calc_change",
        contentType : 'application/json',
        data : JSON.stringify(dto),
        type : 'POST',
        success: function(data) {
            document.getElementById('visits').innerHTML = data[0];
            document.getElementById('slaves').innerHTML = data[1];
        },
        error:  function(xhr, str){
            alert('Возникла ошибка: ' + xhr.responseCode);
        }
    });
}

Routes

Route::post('/calc_change',['uses'=>'PagesController@calc_change','as'=>'calc_change']);

Controller

public function calc_change(Request $request){
    $data = array();
    $data[]=1;
    $data[]=2;
    //dd($data);
    return response()->json($data);
}
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65

1 Answers1

0

You have to add this code before ajax call in jquery section

 $.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
Niket Joshi
  • 739
  • 5
  • 23