NickCourage
3 months ago

How can I use AJAX to get a list of records in the view for a jQuery array?

Posted 3 months ago by NickCourage

I'm trying to populate a jQuery array with the values from a column of a table. I tried:

var dataTitle = {!! json_encode(array_column($dataTitles->toArray(), 'my_data_title')) !!}

I now want to use ajax to do this. I've created a route that references a function in the controller and returns the data in JSON:

    public function getDataTitles(){

        $dataTitles = DataTitle::all();

        return response()->json(['data' => $dataTitles]);


    }

I've create the route for it to use as the URL parameter and have tried:

url = "{{route('get-data-titles')}}";

$.ajax({
    url: url,
    type:'get',
    dataType:'json',
    success:function (response) {

        console.log(data)
    }
})     

but am getting nothing. I've spent some time trying to Google and search for similar examples I can use but so far haven't found anything

Please sign in or create an account to participate in this conversation.