skip to Main Content

I get data from Magento database and I need to return it as an Json object and add data to the table in view. I’m getting a html response, not a valid json.

This is the error message :-

DataTables warning: table id=order_list – Invalid JSON response. For
more information about this error, please see
http://datatables.net/tn/1

Here is my controller code:

public function prepareJson(){

    $orders = Mage::getModel('sales/order')->getCollection()->setOrder('created_at', 'DESC')->setPageSize(25);
    $total_records = $this->orderModObj->get_total();
    $tableData = array();

    $jsonObj =new stdClass();
    $jsonObj->sEcho = 5;
    $jsonObj->iTotalRecords = $total_records;
    $jsonObj->iTotalDisplayRecords = $total_records;

    foreach ($orders as $ordercus) {
        array_push($tableData, $ordercus);
    }
    $jsonObj->aaData = array($tableData);

    $tableJSON = json_encode($jsonObj);
    return $tableJSON;
}

Here is my view code

<table id="order_list" class="table table-bordered table-striped" data-page-length="25">

Here is the json structure

{
"sEcho": "5",
"iTotalRecords": 2708,
"iTotalDisplayRecords": 2708,
"aaData": [
    ["300000374", "16-11-2017", "16-11-2017", "title ", "charith m ", "<img src="http://mysite.lk/dif.gif" class="image" alt="profile picture">charith", "AUD 100.00", "<div class="processing">Processing</div>", "<a class="tb-edit" href="http://mysite.lk/view/300000374">View </a>"]

]
}

Here is the javascript code

$(document).ready(function() {

$('#order_list').DataTable( {

    "processing": true,
    "serverSide": true,

    url: BASE_URL + 'order/prepareJson',
    method: 'post',
    data: { get_param: 'value' },
    dataType: 'json'
    //"ajax": " "
} );
$.ajax({
    success: function (feedback) {
        //console.log("feedback", feedback);
        $("#order_list").append($(feedback));
        }
    });
});

2

Answers


  1. You need to either print() or echo the json and not return it.

    $table_json = json_ecode($data);
    echo $table_json; 
    
    Login or Signup to reply.
  2. You do not have a valid JSON response.

    Copy and paste your JSON here:

    https://jsonlint.com/

    You will see that your issue is the lack of proper quotes with your image tag. Even made Stack Overflow’s code highlighter confused.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search