403在CodeIgniter中使用AJAX时出现禁止错误 [英] 403 Forbidden error using ajax in CodeIgniter

查看:0
本文介绍了403在CodeIgniter中使用AJAX时出现禁止错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AJAX在textbox自动完成中显示名字,但我的AJAX URL不起作用。它每次都会显示在网络选项卡中

403已禁用。

我尝试了AJAX URL,如下所示

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

但仍显示相同的错误。

我的.htAccess代码

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

我的基本URL$config['base_url'] = 'http://localhost/test/';

我的看法

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

constome.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);
                   }
               });
            });
    });

控制器

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);
}

模型

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;
            } 

}

推荐答案

希望这对您有帮助:

首先确保您在config.php中将CSRF内标识设置为FALSE;

 $config['csrf_protection'] = FALSE;
 $config['csrf_token_name'] = 'csrf_test_name';
 $config['csrf_cookie_name'] = 'csrf_cookie_name';
或者,如果不想将其设置为FALSE,只需在AJAX调用中传递csrf_token_nameget_csrf_hash数据 如下所示:

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

您的基本url似乎有问题,因此将此行代码放入页面的页眉,并使用BASE_URL,如下所示

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

您的AJAX应该是这样的:

$(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);
                   }
               });
            });
    });

这篇关于403在CodeIgniter中使用AJAX时出现禁止错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆