Home > database >  I'm getting "undefined" error when trying to response json with ajax in Laravel
I'm getting "undefined" error when trying to response json with ajax in Laravel

Time:11-19

Status code still return 200. I tried to open the route address with new tab and get a blank page. Nothing is logged in console. Here is my code

public function statistics(Request $request)
{
    if($request->ajax())
    {
        $bookings = $this->bookingModel->whereBetween('booked_time', [$request->date_from, $request->date_to])->paginate(10);

        return response()->json([
            'code' => 200,
            'component' => view('manage.dashboard.booking_tbl')->with(['bookings' => $bookings])->render(),
        ],200);
    }
}

let table_url = '{{ route('booking_statistics') }}';
        $.ajax({
                type:'get',
                url:table_url,
                dataType:'json',
                data:{'date_from': date_from, 'date_to': date_to},
                success:function(data){
                    console.log(data);
                    if(data.code === 200){
                        $('.booking_table').html(data.component);
                    }
                },
                error: function(xhr){
                    var err = xhr.responseText;
                    alert(err.error);
                }
            });

CodePudding user response:

You may need to convert the blade to html

view('manage.dashboard.booking_tbl')
    ->with(['bookings' => $bookings])
    ->html()

CodePudding user response:

monitor xhr request in your console when the ajax request is sending and see if the request is fine and check 'request' and 'response' of the ajax request. your javascript code seems to be fine. there must be something else.

  • Related