Daniel
Daniel

Reputation: 179

Data Tables- Json data could not be parsed in Symfony2

I keep getting this error:

DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

I'm not sure what the problem is. Is the routing that is incorrect? I verified the json file that is generated with http://jsonlint.com/ at it is valid.

Controller: public function indexAction($id) {

    return $this-render('CetiucValidateSurveyBundle:Validate:validatespreadsheet.html.twig');



}

The twig(view) file that has the javascript and the table.

validatespreadsheet.html.twig':

<table id="myDataTable" >
   <thead>
   <tr>
       <th>Company name</th>
       <th>Address</th>
       <th>Town</th>
   </tr>
   </thead>
   <tbody>



   </tbody>

the javascript for retrieving data for the table from the controller:

<script language="JavaScript" type="text/javascript">
$(document).ready(function () {
    $('#myDataTable').dataTable(

            {
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": "{{ path('CetiucValidateSurveyBundle_renderJson')}}"
            }

    );

});

The method in the controller for returning the data to the view

public function renderJsonAction(Request $request)
{


    $arr = array ('aaData' => array(
        array('3','35','4', '$14,500', '$15,200','$16,900','5','1'),
        array('1','16','4', '$14,200', '$15,100','$14,900','Running','1'),
        array('5','25','4', '$14,500', '$15,600','$16,900','Not Running','1')
    )
    );

    $post_data = json_encode($arr);

    return new Response( $post_data,200,array('Content-Type'=>'application/json'));//make sure it has the correct content type

}

This is the routing entry for the action returning the json

CetiucValidateSurveyBundle_renderJson:
   defaults: { _controller: "CetiucValidateSurveyBundle:Validate:renderJson" }
   pattern:   /json
   requirements: { _method: POST }

Upvotes: 1

Views: 1878

Answers (1)

Carlos Granados
Carlos Granados

Reputation: 11351

Don't use

requirements: { _method: POST }

Also, are you sure you want to use bServerSide = true?

Upvotes: 1

Related Questions