title: Datatables: Uncaught TypeError: Cannot read property 'length' of undefined
body: <p>I'm attempting to use an ajax source with Datatables, and I've run into some errors in doing. Previously Ajax was not being used with Datatables, and they were working fine, but upon trying to use Ajax and JSON I have some errors.</p>
  
  <p>The error I am recieving is the following:</p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'length' of undefined</p>
  </blockquote>
  
  <p><strong>Edit: Upon using the revised code directly below this text, this error is no longer present but DataTables are still broken (no searching, pagination, sorting, etc...). It may help to have a live example, so try this site: <a href="http://fogest.com/test/index.php" rel="nofollow noreferrer">fogest.com/test</a></strong></p>
  
  <p>To create the tables when the page loads here is the code:</p>
  
  <pre><code>$(document).ready(function() {
      $('#trades').dataTable( {
          "sDom": "&lt;'row'&lt;'span6'l&gt;&lt;'span6'f&gt;r&gt;t&lt;'row'&lt;'span6'i&gt;&lt;'span6'p&gt;&gt;",
          "sPaginationType": "bootstrap",
          "bProcessing": true,
          "bServerSide": true,
          "aoColumns": [
              { "mData": "id" },
              { "mData": "Minecraft_Username" },
              { "mData": "Block_Name" },
              { "mData": "Quantity" },
              { "mData": "Cost" },
              { "mData": "Trade_Status" },
            ],
          "sAjaxSource": "test.php"
      } );
  } );
  </code></pre>
  
  <p>And sAjaxSource test.php contains the following:</p>
  
  <pre><code>&lt;?php 
  $tableName = "mctrade_trades";
  $result = mysql_query("SELECT `id`, `Minecraft_Username`, `Block_Name`, `Quantity`, `Cost`, `Trade_Status` FROM $tableName");
  
  $data = array();
  while ( $row = mysql_fetch_assoc($result) )
  {
      $data[] = $row;
  }
  header("Content-type: application/json");
  echo json_encode( $data );    
  
  ?&gt;
  </code></pre>
  
  <p>The output of test.php:</p>
  
  <pre><code>[{"id":"1","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"100","Trade_Status":"1"},{"id":"2","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"1002","Trade_Status":"1"},{"id":"3","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"1035","Trade_Status":"1"},{"id":"4","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"1035","Trade_Status":"1"},{"id":"5","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"100","Trade_Status":"2"},{"id":"6","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"100","Trade_Status":"2"},{"id":"7","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"10000","Trade_Status":"2"}]
  </code></pre>
  
  <p>The table is generated correctly but due to this error, there is text saying "Processing right above the table, and you cannot use any of the functions of the datatable, such as searching.</p>
  
  <p>Here is an image of what the table looks like using the above JSON:
  <img src="https://i.stack.imgur.com/Ujt9o.png" alt="Example table output"></p>
  
  <p>I'm assuming the error is in my JSON output, but I do not exactly know what is wrong with it, nor what I should do to fix it. I'm pretty new to Web Development and implementing Datatables has been quite the learning curve!</p>