samedi 24 août 2019

Laravel : can't read data from my Ajax jQuery code in my controller

i'm sending the section id in my ajax function,but i can't receive this data in my controller function.need your help please.

This is my jQuery:

$(document).ready(function(){
    $.ajaxSetup({
    headers: {

        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

    $('#getGroupe').change(function(){
        var section = jQuery('#getGroupe').val();
        $.ajax({
            url: "/ajax",
            type: "POST",
            data: {                   
                section: JSON.stringify(section),                   
            },

            success: function(){
                alert(section);
            },
            error: function(){
                alert("error");
            }
        });

        $.post(" ",function(data){
                $('#groupe').empty().html(data);

        });
    });
});

Web:

Route::post('ajax' , 'SeanceController@ajaxfct');

My function in the controller:

public function ajaxfct(Request $request){

    $id = $request->section;
    $groupes = \DB::table('groupes')
                ->where('section_id','=',$id)
                ->get();

    return view('seance.groupe')->with([
        'groupes' => $groupes
    ]);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire