mardi 13 février 2018

laravel ajax not passing post value

While trying to ajax to my server I ran into a problem. When making reqeusts with just the get method my script was working fine, but when I do it with post only url paramaters seem to pass. So my qeustion is: why doesn't it work, and how can I fix it?

web.php:

Route::prefix('ajax/')->group( function($request) {
     Route::prefix('company_systems/{company_system}/')->group(function ($request) {
        Route::post('reservations/available_times', 
        'Systems\Reservation\ReservationController@available_times');
    });
});

The method in the controller

/**
 * Get the available hours and the occupation
 *
 * @todo    Make use of local timezones
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function available_times(Request $request)
{
    $input = $request->all();
    return response()->json($request->company_system); // Returns correct value

}

That seems to work fine but when I change the return to the code below it doesn't.

return response()->json($request->date); // Returns incorrect value

In my app.js I have the token set

// Set up ajax with the token
$.ajaxSetup({
    headers: {
         'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

The actual reqeust:

$.ajax({
    async: false,
    type: "POST",
    url: "/ajax/company_systems/1/reservations/available_times",
    data: {'date': 1},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        console.log(data);
    }
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire