Reading a very simple ajax request in Laravel

- 1 answer

Ad

I lately managed to get a simple ajax post to work but can't get any of the data in the controller :

Ajax :

function verify(event) {
        var title = event.title;
        var start = event.start.format("h:m");
        $.ajax({
            url: "/admin/timetable/verify",
            headers: {
                'X-CSRF-TOKEN': $('#crsf').val()
            },
            type: "post",
            contentType: "application/json; charset=utf-8",
            data: {type : 'hi',titles : title},
            dataType: "json",
            success: function(response){
                if (response['state']==='0')
                    toastr.error('Are you the 6 fingered man?'+response['msg']);

                if (response['state']==='1')
                    toastr.info('Are you the 6 fingered man?');
            },
            error : function(e){
                console.log(e.responseText);
            }

        });

    }

Controller :

 $d = Request::all();
        dd($d);
        return response()->json(['state'=>'0','msg'=>$d['titles']],200);

I tried Request all, Input all, Input::json()->all() .. nothing works always null or empty array [] ! I'm just trying to read the data sent from the ajax form !

Ad

Answer

Ad

I faced this lately. The problem (I don't know why) was about Get and POST.

Just transform route to a GET, make the ajax type as GET, and try with a very simple Input::all.

 public function verifyClassroom(){
        $Data = Input::all();
        dd($Data);
}
Ad
source: stackoverflow.com
Ad