Codeigniter ajax post 您请求的操作不被允许

Codeigniter ajax post the action you requested is not allowed

我正在尝试调用 ajax 一旦用户在按钮上有 onclick,然后按钮会将 id 传递给控制器​​以在我的模态 [=34] 中输出结果=].但是,当我调试时,它显示不允许执行您请求的操作。

我可以在我的代码中做些什么吗?

查看:

<input type="" name="" value="VIEW" class="btn btn-primary m-t-15 waves-effect" data-toggle="modal" data-target="#defaultModal" onclick="refresh_detail('.$row["Task_ID"].')">

jQuery / AJAX 代码:

<script type="text/javascript">
  function refresh_detail(id){
    $.ajax({
      url : "<?= base_url()?>admin/Admintask/get_task_detail",
      type : "POST",
      dataType : "json",
      data : {"TaskID" : id},
      success : function(data) {
        // do something
      },
      error : function(data) {
        // do something
      }
    });
  }
</script>

控制器:

public function get_task_detail(){
  echo $this->input->post('TaskID');
}

第 1 步 - onclick="refresh_detail(<?php echo $your_id ?>)"

第 2 步 - 如果您使用 HTTP,请将配置文件中的 $config['cookie_secure'] 设置为 FALSE。

第 3 步 - 设置 $config['csrf_protection'] = false;

@Marcus 由于 CSRF 保护而收到 403,您可以将 URI 添加到 config.php 文件中的“csrf_exclude_uris”。 步骤:

  1. 打开你的“application/config/config.php

  2. 转到第 457 行或它写的地方 $config['csrf_exclude_uris'] = array();

  3. 在数组中加入你的URI,比如

    $config['csrf_exclude_uris'] = array('admin/Admintask/get_task_detail');

    注意:确认来自 routes.php

    的 URI

这将从 CSRF 验证中排除 URL。您可以从 here.

中了解更多信息

@Marcus 你需要用你的 ajax 代码发送 CSRF 令牌,比如 data : {"TaskID" : id,"<?= $this->security->get_csrf_token_name(); ?>":"<?= $this->security->get_csrf_hash(); ?>"}