使用 ajax 时如何为 chrome 浏览器修复 Codeigniter Csrf_token?

How to fix Codeigniter Csrf_token for chrome browser when using ajax?

我在 Jquery Ajax[=47= 中使用 CodeIgniter3 ] 用于将一些数据插入数据库。

然而我使用csrf_token在用户插入如下内容时制作安全表单数据。

问题 我无法在 Chrome 浏览器上将数据插入数据库,但在 Firefox 中可以。

错误 403 禁止

<div id="container">
        <h1>An Error Was Encountered</h1>
        <p>The action you have requested is not allowed.</p>    </div>
</body>

这是我在配置文件中的配置:

$config['global_xss_filtering'] = TRUE;
$config['csrf_protection'] = TRUE;
$config['csrf_regeneration'] = TRUE;
$config['csrf_token_name'] = 'csrf_token_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_exclude_uris'] = array();
$config['csrf_expire'] = 7200;

然后我使用控制器将一些数据插入到数据库中

public function post() {

        $new_token = $this->security->get_csrf_hash();
        $this->form_validation->set_rules($this->ads_m->post_rule);
        if ($this->form_validation->run() == FALSE) {
            echo json_encode(array('res' => FALSE));
        } else {

            $data = array(
                'name' => $this->input->post("p_name"),
                'user_id' => $this->user->user_id(),
                'price' => $this->input->post('p_price'),
                'addr' => $this->input->post('p_add'),
                'des' => $this->input->post('desc'),
                'status' => 1,
            );
            $where = NULL;
            $this->ads_m->insert_post($data, 'ads', $where);
            if ($this->ads_m->insert_check() == TRUE) {
                echo json_encode(array('res' => TRUE, 'token' => $new_token));
            } else {
                echo json_encode(array('res' => FALSE));
            }
        }
    }

这是我的模型

public function insert_post($data, $table, $where = FALSE) {

        $this->db->set($data);
        if ($where) {
            $this->db->where($where);
        }
        $this->db->insert($table);
    }

这里是Ajax发送数据

$.ajax({
type: "post",
url: '<?php echo base_url('ads/post'); ?>',
data: $("#post_form").serialize(),
dataType: "json",
cache: false,
beforeSend: function () {
   }, success: function (data) {
          alert("you are successfully ");
   }
 });
}

HTML 表单数据

 <?PHP echo form_open(base_url('ads/post'),array("class" => "form_horizontal","id" => "post_form")); ?>
  <div class="controls form-group-sm">
    <label class="label-info"> Name </label>
    <?PHP echo form_input('p_name', '', 'class="form-control" id="p_name" '); ?>
    <label class="label-info">Price</label>
    <?PHP echo form_input('p_price', '', 'class="form-control" id="p_prce" '); ?>
    <label class="label-info">Location</label>
    <?PHP echo form_input('p_locat', '', 'class="form-control" id="p_locat" '); ?>
  </div>
 <?PHP echo form_close(); ?>

所有 Codeigniter 开发者请注意,当您使用 Ajax 发送数据和 select 使用 CSRF 保护从数据库发送数据时,请使用

<?php echo $this->security->get_csrf_token_name(); ?>': '<?PHP echo $this->security->get_csrf_hash(); ?>

对您的表单有效,因为当您向服务器请求时,CSRF 保护将检查您的 CSRf 以确保它是否相等以及 Csrf 是否有效 CI会让你的Ajax请求。

这是我使用 Ajax 和 Csrf 保护将数据添加到服务器的最后一个任务,现在它运行良好

  $.ajax({
        type: "post",
         url: "<?php echo base_url('ads/post'); ?>",
         data: {
         '<?php echo $this->security->get_csrf_token_name(); ?>': '<?PHP echo $this->security->get_csrf_hash(); ?>',
         p_name: $("#p_name").val(),
         p_price: $("#p_price").val(),
         p_addr: $("#p_addr").val(),
         p_des: $("#p_des").val(), 
         p_located: $('#testing option:selected').val(), 
              },
           dataType: "json",
           cache: false,
           beforeSend: function () {
               $('#logmodel').modal('show');
               $("#loading_modal").modal({
               backdrop: 'static',
               keyboard: false,
           });
       }, success: function (data) {
            if (data.res === false) {
               $("#m_errors").css({'display': 'inline'});
               $("#loading_modal").modal({
               backdrop: 'static',
               keyboard: false,
             });
           } else {
              $("#loading_modal").modal('hide');
              $('#modalAds').modal('hide');
              $("form input[type=text]").val("");
              $("textarea").val("");
         }
      }
    });