Hen*_*eak 4 ajax jquery codeigniter-3
我在Jquery Ajax中使用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>
Run Code Online (Sandbox Code Playgroud)
这是我在配置文件中的配置:
$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;
Run Code Online (Sandbox Code Playgroud)
我用控制器将一些数据插入数据库
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模特
public function insert_post($data, $table, $where = FALSE) {
$this->db->set($data);
if ($where) {
$this->db->where($where);
}
$this->db->insert($table);
}
Run Code Online (Sandbox Code Playgroud)
这是用于发送数据的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 ");
}
});
}
Run Code Online (Sandbox Code Playgroud)
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(); ?>
Run Code Online (Sandbox Code Playgroud)
所有Codeigniter开发人员请注意,当您使用Ajax发送数据并使用CSRF保护从数据库中选择数据时,请使用
<?php echo $this->security->get_csrf_token_name(); ?>': '<?PHP echo $this->security->get_csrf_hash(); ?>
Run Code Online (Sandbox Code Playgroud)
对您的表单有效,因为当您请求服务器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("");
}
}
});
Run Code Online (Sandbox Code Playgroud)