title: fetch data from database using ajax codeigniter
body: <p>In this code after clicking on button it should fetch the data from database and display it dynamically within appropriate div using jquery and ajax but i am not getting any output, below is my code:</p>
  
  <p><strong>controller</strong>  </p>
  
  <pre><code>defined('BASEPATH') OR exit('No direct script access allowed');
  
  class Afcks extends CI_Controller 
  {
  public function __construct()
  {
      parent::__construct();
      $this-&gt;load-&gt;model('afcks_search','am');        
  }
  function index()
  {
      $this-&gt;load-&gt;view('afcks_home');
  }
  function search_course()
   {
          $result=$this-&gt;am-&gt;search_course();
          echo json_encode($result);
   }
  }
  </code></pre>
  
  <p><strong>model</strong></p>
  
  <pre><code> class Afcks_search extends CI_Model
   {
      function search_course()
      {
          $query=$this-&gt;db-&gt;query("SELECT course_name FROM courses");
          return $query-&gt;result();
       }
  }
  </code></pre>
  
  <p><strong>view</strong></p>
  
  <pre><code> &lt;head&gt;
    &lt;script&gt;
   $(document).ready(function(){
          $("button").click(function(){
  
              $.ajax({
                  url:"&lt;?php echo base_url(); ?&gt;/afcks/search_course";
                  type:"POST",
                  dataType:"json",
                  data:req,
                  success:
                  function(data)
                  {
                      var str='';'
                      str+='&lt;ul&gt;';
                      for(var i = 0; i&lt; data.length; i++)
                      {
                          str+='&lt;li&gt;'+data.course+'&lt;/li&gt;';
                      });
                      str+='&lt;/ul&gt;';
                      $('#course').html(str);
                  }
              });
          });
      });
      &lt;/script&gt;  
   &lt;/head&gt;
   &lt;body&gt; 
  &lt;button&gt; click me &lt;/button&gt;
  &lt;div id="course"&gt; &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>