0

My JSON output from controller reads like this:

{"subscribers":"[{\"code\":\"Code1\",\"name\":\"Name1\",\"id\":539,\"companyId\":\"CD1\",\"acctNum\":\"Dell\"},{\"code\":\"Code2\",\"name\":\"Name2\",\"id\":540,\"companyId\":\"CD2\",\"acctNum\":\"Dell1\"}]"}

It is the output variable "data" from the controller 'three.htm' :

$("#getName").click(function() {
     $.getJSON("three.htm", function(data) {
         })
         .fail(function( jqxhr, textStatus, error ) {
         var err = textStatus + ', ' + error;
         alert(err);
         console.log( "Request Failed: " + err);
         })
         .success(function(data){
             console.log("loadDataTable >>  "+JSON.stringify(data));
             loadDataTable(data);
         })
 });

 function loadDataTable(data){
     $("#recentSubscribers").dataTable().fnDestroy(); 
     var oTable = $('#recentSubscribers').dataTable({
         "aaData" : data,
         "processing": true,
        "aoColumns" : [
           {"mData" : "code" },
          { "mData" : "name" },
          { "mData" : "id" },
          {"mData" : "companyId" },
          {"mData" : "acctNum" }
           ]
        });
     }

However, the datatable isn't showing any results.

Pretty much stuck here after trying many approaches. Help please.

1 Answer 1

1

Your JSON is basically just an object with one item, subscribers, holding a long string. You need to parse that string to JSON before it can be inserted to dataTables :

...
"aaData" : JSON.parse(data.subscribers),
...

Your code working here -> http://jsfiddle.net/x5n94dqv/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Worked so well like a charm :) Was stuck on this small part for pretty long time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.