Datatable Ajax call not populating data

Datatable Ajax call not populating data

harshdeepharshdeep Posts: 6Questions: 2Answers: 0

Hi,
I am trying to GET data from C# Webservice and populate Jquery Datatable with the reponse. Here is my code -

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>WFM Product Comparison</title>
  
  <link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet">  
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>    
                
<script>
   
$.ajax({
    url: 'http://<<myhostname>>.com/WebSite/WebService1.asmx/GetData',    
    contentType: "application/json; charset=utf-8",    
    success : function(data) {
        alert(data.d);
        
        var table = $('#example').DataTable( {
            data: data,
            columns: [
                    { data: 'yoyid'},
                    { data: 'year'},
                    { data: 'system_stability'},
                    { data: 'productivity'},
                    { data: 'reuse'},
                    { data: 'automation'},
                    { data: 'onsite'}
        ]
}); } });
    
    </script>
</head>

<body>
 <table id="example" class="display" style="width:100%">
    <thead><tr>
     <th>yoyid</th>        
        <th>year</th>
        <th>system_stability</th>
        <th>productivity</th>
        <th>reuse</th>
        <th>automation</th>
        <th>onsite</th>
     </tr>
     </thead>                

    </table>
</body>

</html>

Response from the Webservice call -

{,…}
d: "[{"yoyid":1.0,"year":2018.0,"system_stability":10.0,"productivity":11.0,"reuse":11.0,"automation":11.0,"onsite":11.0}]"

On alert, this same response is getting displayed, however DataTable is not populating any data.

Please suggest.

Answers

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Since you data object is in data.d that's what you will want to populate Datatables with. For example:

            var table = $('#example').DataTable( {
                data: data.d,
                columns: [
    .....
    

    Kevin

  • harshdeepharshdeep Posts: 6Questions: 2Answers: 0

    Thanks for the reply Kevin.
    However, I am getting following error when I give data.d

    DataTables warning: table id=example - Requested unknown parameter 'yoyid' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
    
  • harshdeepharshdeep Posts: 6Questions: 2Answers: 0

    I run the debugger - https://debug.datatables.net/uxeyaz

    I can see that the response from webservice is broken down in single character each and being fed as individual row to the datatable. And hence table is not able to find the parameter yoyid thus giving error.
    Why the response is getting broken down into character and how can I fix this?

  • harshdeepharshdeep Posts: 6Questions: 2Answers: 0

    Fixed it by using JSON.parse(data.d)

This discussion has been closed.