2017-04-26 5 views
0

Ich benutze Laravel 5.2 und Yajra Datatable, um Tabellen mit Ajax zu implementieren. Ich folgte diesem Tutorial: https://datatables.yajrabox.com/ aber am Ende habe ich "ungültige JSON Antwort" und ich weiß nicht warum.Ungültige JSON Antwort Laravel Datatable

Mein Controller:

class ContactController extends Controller 
{ 

public function index() 
{ 

    return view("contacts.list"); 
} 

public function data() 
{ 
    $contacts = Contact::select(array(
      'NOM', 'PRENOM' 
     )); 

     return(Datatables::of($contacts)->make(true)); 
} 
} 

Meine Ansicht:

<table class="table table-bordered" id="users"> 
    <thead> 
     <tr> 
      <th>NOM</th> 
      <th>PRENOM</th> 
     </tr> 
     </thead> 
</table> 

<script type="text/javascript"> 
    var $ = jQuery.noConflict(); 

    $(document).ready(function() { 
    oTable = $('#users').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": "{{ route('contacts.data') }}", 
     "columns": [ 
     {data: 'NOM', name: 'NOM'}, 
     {data: 'PRENOM', name: 'PRENOM'} 
     ] 
    }); 
    }); 
</script> 

Meine Routen:

Route::resource('contacts', 'ContactController'); 
Route::get('contacts/data', ['as' => 'contacts.data', 'uses' => '[email protected]']); 

Das Ergebnis

dd(Datatables::of($contacts)->make(true)) 
in Index

in meinem Controller, wenn ich es tun() ist: enter image description here

+0

ich das gleiche Problem haben, haben Sie das Problem beheben? Ich erhalte diesen Fehler: DataTables-Warnung: Tabelle ID = Benutzer-Tabelle - ungültige JSON-Antwort. Weitere Informationen zu diesem Fehler finden Sie unter http://datatables.net/tn/1 – lewis4u

+0

Überprüfen Sie die Antwort im Netzwerk Registerkarte kann es '

Antwort

0

Ich denke, es sein sollte:

return(Datatables::of($contacts->get())->make(true)); 
+0

Ja Es scheint logisch, aber ich habe das gleiche Ergebnis mit get() – K4tn1x