phpajaxlaravel

Laravel is sending GET instead of POST request


I have a problem with Laravel and POST request. My route is defined as POST and method used in AJAX is POST but it keeps sending GET requests. If I change route to undefined route then it sends POST, but if I aim to this route defined as POST it sends GET request.

AJAX:

$.ajax({
    method: "POST",
    url: "{{ url('admin/rentacar/save_availability_for_d') }}",
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    data: $(form).serialize(),
    dataType: "json",
    success(result){
        //
    }
});

Route is defined as:

Route::post('save_availability_for_d', [
    'as' => 'save_availability_for_d', 
    'uses' => 'RentacarController@saveCarAdjustment'
]);

CSRF token is included in meta tags:

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

This is what happens in console when I try to send AJAX request:

XHR finished loading: GET "http://www.carsrental.me/public/me/admin/rentacar/save_availability_for_d".

and this is what happens if I append just one random character at the end to aim for route that doesn't exists

XHR failed loading: POST "http://www.carsrental.me/public/admin/rentacar/save_availability_for_dd".

Solution

  • Try this and also don't forget to clear the cache

    $.ajax({
          url: '{{route('save_availability_for_d')}}',
          dataType: 'json',
          type: 'POST',
          data: $(form).serialize(),
          success: function (result) {
          }
    });