3

I am displaying the first name in my textbox autocomplete using ajax but my ajax URL is not working. It's every time showing in the network tab

403 Forbidden.

I tried ajax URL like this

 url:baseUrl + "/index.php/Employee_control/search_with_emp_name",
 url:baseUrl +"/Employee_control/search_with_emp_name",

But still showing the same error.

my .htaccess code

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

My base URL is $config['base_url'] = 'http://localhost/test/';

My view

<input type="text" class="form_control" name="employee_name" id="employee_name">

custome.js

var getUrl = window.location;
var baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];


    $(document).ready(function() {
       $("#employee_name").keyup(function() {
           var emp_name = $('#employee_name').val();
              $.ajax({
                   type: "POST",
                   url:baseUrl + "/index.php/Employee_control/search_with_emp_name",
                   data: {
                      emp_name: emp_name
                   },

                   success: function(html) {
                      alert(html);
                   }
               });
            });
    });

Controller

public function search_with_emp_name(){
        echo $emp_name = $this->input->post('emp_name');
       $get_result=$this->Employee_model->search_emp_name($emp_name);
       print_r($get_result);
}

Model

public function search_emp_name($emp_name){
          $this->db->like('firstname', $emp_name, 'both'); 
          $query = $this->db->get('tbl_employee');
          $result = $query->result();
        if($result)
            {
              return $result;
            }
            else 
            {
              return 0;
            } 

}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
user9437856
  • 2,360
  • 2
  • 33
  • 92
  • Possible duplicate of [Ajax call on CodeIgniter controller results in 403 Forbidden](https://stackoverflow.com/questions/27727418/ajax-call-on-codeigniter-controller-results-in-403-forbidden) – Sparky Jun 19 '18 at 20:18
  • Also see: https://stackoverflow.com/q/7348383/594235 – Sparky Jun 19 '18 at 20:19

1 Answers1

8

Hope this will help you :

First make sure your you have set csrf token to false in your config.php;

 $config['csrf_protection'] = FALSE;
 $config['csrf_token_name'] = 'csrf_test_name';
 $config['csrf_cookie_name'] = 'csrf_cookie_name';

OR if you don't want to set it to false just pass csrf_token_name and get_csrf_hash with data in your ajax call like this :

 data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>'},

It seems that problem is with your base url so Put this line of code in your header of the page and use BASE_URLlike this

<script type="text/javascript">
     const BASE_URL = "<?php echo site_url();?>";
</script>
<script src="<?=site_url('your-js-path/js/custome.js');?>"></script>

Your ajax should be like this :

$(document).ready(function() {
       $("#employee_name").keyup(function() {
           var emp_name = $('#employee_name').val();
              $.ajax({
                   type: "POST",
                   url: BASE_URL+"Employee_control/search_with_emp_name",
                   data: {emp_name: emp_name},

                   success: function(html) {
                      alert(html);
                   }
               });
            });
    });
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • my custome.js file is outside of the application. – user9437856 Jun 19 '18 at 18:08
  • so what is problem just provide the path as given in my answer – Pradeep Jun 19 '18 at 18:09
  • It's showing 404 not found – user9437856 Jun 19 '18 at 18:14
  • I just want to know what is the issue with my baseUrl which I added in the script? – user9437856 Jun 19 '18 at 18:15
  • so give the correct path to your file and do you have enable csrf protection in your config.php – Pradeep Jun 19 '18 at 18:19
  • Yes, csrf protection path is True. I set the right path. I think your baseurl script is not working. can you help me with the issue in my base URL? – user9437856 Jun 19 '18 at 18:23
  • that is the problem just put csrf protection to false – Pradeep Jun 19 '18 at 18:25
  • Yes, that's correct. I use only my base URL and I set csrf protection false and this time it's showing right. It's working. Please update the answer – user9437856 Jun 19 '18 at 18:34
  • Cool! Thanks for your help, upvote from my side. Pradeep I need one more help in the codeignator. I will ask the new question and I will tag you please reply – user9437856 Jun 19 '18 at 18:40
  • sure i will help you if i can – Pradeep Jun 19 '18 at 18:41
  • My issue is, I am able to display the record list and I set view on each list when user click on view the I have to display the full message which will come from database....List are displaying but when I click on popup it's not displaying the message – user9437856 Jun 19 '18 at 18:43
  • obviously you have foreach loop to show your records, loop also your modal within it and give record id as id of modal making it unique id , then on click call this modal – Pradeep Jun 19 '18 at 18:52
  • Actually, I am getting the output in view source but not displaying in the popup. In the popup, It displays the last record – user9437856 Jun 19 '18 at 18:58