title: Codeigniter - Download CSV using AJAX
body: <p>i am having problem in downloading a CSV file using codeigniter, i want when user press the export button file should start downloading but what happens is response is generated in ajax called function as shown in screen shot. I know i am missing some thing small but i can not get my head straight over it what to do.</p>
  
  <p><strong>AJAX RESPONSE SHOWN IN FIREFOX CONSOLE</strong></p>
  
  <p><img src="https://i.stack.imgur.com/VvwXp.png" alt="AJAX RESPONSE"></p>
  
  <p><strong>HTML BUTTON</strong></p>
  
  <pre><code> &lt;button id="exportData" name="exportData" class="btn btn-primary btn-lg" &gt;Download CSV&lt;/button&gt;
  </code></pre>
  
  <p><strong>AJAX CALL</strong></p>
  
  <pre><code>$(document).on('click','#exportData',function(e){
  e.preventDefault();
  
  var jawad = $( ".jawad" ).serialize();
  $.ajax({
  url         : '&lt;?=base_url();?&gt;command/getReport',
  type        : 'POST',
  data        :  {str:jawad,procName:$('#procName').val()}
  });
  
  });
  </code></pre>
  
  <p><strong>PHP FUNCTION:</strong></p>
  
  <pre><code>function getReport()
  { 
   //removing above lines as not necessary
  
  $jawad =  $query-&gt;result_array();
  
  //pass it to db utility function
  $new_report = $this-&gt;dbutil-&gt;csv_from_result($query);
  
  $this-&gt;load-&gt;helper('download');
  force_download('csv_file.csv', $new_report);
  }
  </code></pre>