Iso B
Iso B

Reputation: 3

JSON showing instead of Datatable following Ajax call to Laravel Controller

I’m fairly new to ajax and json and would really appreciate some help. I’m making an Ajax call to a Laravel Controller to return some fields from a database table called "subjects" and display them in a DataTable in a Laravel View. The problem is that when I open the view, what is see is JSON rather than the Datatable.

Here’s what’s returned in the view subjects/index:

{"draw":0,"recordsTotal":8,"recordsFiltered":8,"data":[{"id":"1","name":"Biology"},{"id":"3","name":"English Language"},{"id":"4","name":"Physics"},{"id":"5","name":"Chemistry"},{"id":"6","name":"Mathematics"},{"id":"7","name":"Mathematics"},{"id":"8","name":"English Language"},{"id":"9","name":"French"}],"queries":[{"query":"select count(*) as aggregate from (select '1' as `row_count` from `subjects`) count_row_table","bindings":[],"time":4.65},{"query":"select `id`, `name` from `subjects`","bindings":[],"time":0.41}],"input":[]}

Here’s the HTML in the view /subjects/index

<table id="subjects_table" class="table table-bordered" style="width:100%">
    <thead>
            <tr>
                <th>Id</th>
                <th>Subject</th>
            </tr>
                </thead>
                <tbody>

                </tbody>
        </table>

Here’s the code in the Laravel Controller:

class SubjectsController extends Controller
{
    public function index()
    {
        $subjects = Subject::select('id', 'name');
        return Datatables::of($subjects)->make(true);
   }
}

Here’s the code making the Ajax call:

$('#subjects_table').DataTable({
      "processing": true,
      "serverSide": true,
      "ajax": "{{route('subjects.index')}}",
      "columns":[
          {"data": "id"},
          {"data": "name"}
      ]
});

Here’s the route definition in web.php:

Route::get('subjects/', 'SubjectsController@index')->name('subjects.index');

Any help you can provide would be really appreciated

Upvotes: 0

Views: 2625

Answers (1)

user10186369
user10186369

Reputation:

You should try this:

Routes

Route::get('subjects', 'SubjectsController@index')->name('subjects.index');
Route::get('getsubjects', 'SubjectsController@getSubjects')->name('subjects.get');

SubjectsController

class SubjectsController extends Controller
{
    public function index()
    {
        return view('subjects.index');
        $subjects = Subject::select('id', 'name');
        return Datatables::of($subjects)->make(true);
   }

   public function getSubjects()
    {

        return \DataTables::of(Subject::query())->make(true);

   }
}

View

    <table id="subjects_table" class="table table-bordered" style="width:100%">
    <thead>
            <tr>
                <th>Id</th>
                <th>Subject</th>
            </tr>
                </thead>
                <tbody>

                </tbody>
        </table>

<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#subjects_table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{{ route('subjects.get') }}',
            columns: [
                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'email', name: 'email'},
            ]
        });
    });
    </script>

Upvotes: 1

Related Questions