title: Refresh CSRF Token on AjaxPOST Datatables: CodeIgniter
body: <p>I'm trying to make datatables work in my website. But when I click search, next page, sorting, it's not working. This is because of the CSRF token not being regenerated.</p>
  
  <p>Here's my code:</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;input type="hidden" id="hash" name="csrf_test_name" value="802daa2efaf69edb83b571d7bf7510aa"&gt;
          &lt;table id="test-table" class="table table-hover"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;No&lt;/th&gt;
                      &lt;th&gt;First Name&lt;/th&gt;
                      &lt;th&gt;Last Name&lt;/th&gt;
                      &lt;th&gt;Phone&lt;/th&gt;
                      &lt;th&gt;Address&lt;/th&gt;
                      &lt;th&gt;City&lt;/th&gt;
                      &lt;th&gt;Country&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              &lt;/tbody&gt;
  
              &lt;tfoot&gt;
                  &lt;tr&gt;
                      &lt;th&gt;No&lt;/th&gt;
                      &lt;th&gt;First Name&lt;/th&gt;
                      &lt;th&gt;Last Name&lt;/th&gt;
                      &lt;th&gt;Phone&lt;/th&gt;
                      &lt;th&gt;Address&lt;/th&gt;
                      &lt;th&gt;City&lt;/th&gt;
                      &lt;th&gt;Country&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/tfoot&gt;
          &lt;/table&gt;
  </code></pre>
  
  <p><strong>JS</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  var table;
  
  $(document).ready(function() {
  
  //datatables
  table = $('#test-table').DataTable({ 
  
      "processing": true, //Feature control the processing indicator.
      "serverSide": true, //Feature control DataTables' server-side processing mode.
      "order": [], //Initial no order.
      "ajax": {
          "url": "http://oss-dev.forexworld.us/oss/user/ajax_receiving",
          "type": "POST",
          data: {
              'csrf_test_name' : '802daa2efaf69edb83b571d7bf7510aa' 
              },
         dataSrc: function ( json ) {
             if(json.csrf_test_name !== undefined) $('meta[name=csrf_test_name]').attr("content", json.csrf_token);
             return json.data;
         }
      },
      "columnDefs": [
      { 
          "targets": [ 0 ], //first column / numbering column
          "orderable": false, //set not orderable
      },
      ],
  
  });
  
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>I can make my datatable work by setting the <code>$config['csrf_regenerate'] = TRUE;</code> to <strong>FALSE</strong> but I don't want that. My guess is that the <code>dataSrc</code> part is not working but I'm not sure.</p>
  
  <p>You can see it in action <strong><a href="http://oss-dev.forexworld.us/oss/" rel="nofollow noreferrer">here</a></strong>.</p>
  
  <p>Any help is highly appreciated. Thanks!</p>