title: How can I refresh DataTable after an ajax success?
body: <p>I have tried different ways to refresh my data table after an AJAX Success, but without luck. I tried draw() and .ajax.reload() function, but still no luck. Do you have any idea how to refresh it?</p>
  
  <p>Here is my code</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;table id="giacenza" class="table table-striped table-bordered"&gt;
     &lt;thead&gt;
        &lt;tr&gt;
           &lt;th&gt;Modello&lt;/th&gt;
           &lt;th&gt;Descrizione&lt;/th&gt;
           &lt;th&gt;Colore&lt;/th&gt;
           &lt;th&gt;Taglia&lt;/th&gt;
           &lt;th&gt;Barcode&lt;/th&gt;
           &lt;th&gt;Quantità&lt;/th&gt;
        &lt;/tr&gt;
     &lt;/thead&gt;
     &lt;tbody&gt;
     &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>JavaScript</p>
  
  <pre><code>$(document).ready(function() {
      function getRecords(){
          $.ajax({
              type: "POST",
              url: "functions/getImported.php",
              success: function(result) {
                  var table = $('#giacenza').DataTable({
                      dom: 'Bfrtip',
                      buttons: [
                          'copy', 'csv', 'excel', 'pdfHtml5', 'print'
                      ],
                      order: [[ 1, "asc" ]],
                  });
                  var data = JSON.parse(result);
                  table.rows.add(data).draw();
              },
              error: function(result) {
                  alert("error: "+result);
              }
          });
      }
      getRecords();
      $("#scarica-articolo").submit(function(event) {
          event.preventDefault();
          var codbarra = $("#codbarra").val();
          $("#scarica-btn").attr("disabled",true);
          $.ajax({
              type: "POST",
              url: "functions/scaricaArticolo.php",
              data: {codbarra:codbarra},
              success: function(result) {
                  if(result = "OK"){
                      new PNotify({
                        title: 'Articolo scaricato!',
                        text: 'L\'articolo è già stato scaricato dalla tabella seguente!',
                        type: 'success',
                        styling: 'bootstrap3'
                      });
                      $("#scarica-btn").attr("disabled",false);
                      getRecords();
                  }else if(result == "KO"){
                      new PNotify({
                          title: 'Oh No!',
                          text: 'La query non è andata a buon fine. Contattare l\'assistenza.',
                          type: 'error',
                          styling: 'bootstrap3'
                      });
                      $("#scarica-btn").attr("disabled",false);
                  }
              },
              error: function(result) {
                  alert("error: "+result);
              }
          });
      });
  });
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  include("db_config.php");
  $mysqli-&gt;set_charset("utf8");
  $codbarra = $mysqli-&gt;real_escape_string($_POST["codbarra"]);
  
  $query = "UPDATE records SET PARESTALLA = PARESTALLA-1 WHERE CODBARRA = ".$codbarra.";";
  $results = array();
  $result = $mysqli-&gt;prepare($query);
  $result-&gt;execute();
  if($result-&gt;affected_rows &gt; 0){
      echo "OK";
  }else{
      echo "KO";
  }
  ?&gt;
  </code></pre>
  
  <p>EDIT:
  I updated my JavaScript script but I cannot resolve my issue. Now I still receive an error similar: "DataTables warning: table id=giacenza - Cannot reinitialise DataTable. For more information about this error, please see <a href="http://datatables.net/tn/3" rel="nofollow noreferrer">http://datatables.net/tn/3</a>"</p>