[英]CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request
I am getting a 403 forbidden error on jQuery.ajax() post request.我在 jQuery.ajax() 发布请求时收到 403 禁止错误。
application/config/config.php应用程序/配置/config.php
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_ghive';
$config['csrf_cookie_name'] = 'csrf_ghive';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = false;
application/views/billing.php应用程序/视图/billing.php
<input type="text" class="txt_csrfname" name="<?= $this->security->get_csrf_token_name(); ?>" value="<?= $this->security->get_csrf_hash(); ?>">
<button id="bill_client"><?php echo lang('pay'); ?></button>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/customers/js/bill.js"></script>
assets/customers/js/bill.js资产/客户/js/bill.js
function add_bill_by_ajax(bill_object,bill_id){
// CSRF Hash
var csrfName = $('.txt_csrfname').attr('name'); // Value specified in $config['csrf_token_name']
var csrfHash = $('.txt_csrfname').val(); // CSRF hash
$.ajax({
url:base_url+"Bill/add_bill_by_ajax",
method:"POST",
data:{
order : bill_object,
sale_id : bill_id,
close_order : 0,
//csrf_test_name: $.cookie('csrf_ghive')
//'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>'
[csrfName]: csrfHash
},
dataType: 'json',
success:function(response) {
if(response>0){
$('.txt_csrfname').val(response.token);
set_new_bills_to_view();
swal({
title: warning,
text: added_bill,
confirmButtonText:ok
});
}
},
error:function(){
console.log("403 Forbidden for ajax error");
swal({
title: warning,
text:'403 Forbidden for ajax error',
confirmButtonText:ok,
allowOutsideClick: false
});
}
});
}
application/controllers/Bill.php应用程序/控制器/Bill.php
function add_bill_by_ajax(){
$bill_details = json_decode(json_decode($this->input->post($this->security->xss_clean('bill'))));
//this id will be 0 when there is new bill, but will be greater then 0 when there is modification
//on previous order
$bill_id = $this->input->post('bill_id');
$close_bill = $this->input->post('close_bill');
$data = array();
$data['token'] = $this->security->get_csrf_hash();
$data['customer_id'] = trim($bill_details->customer_id);
...
Console errors控制台错误
jquery-3.3.1.min.js:2 POST http://localhost/Bill/add_bill_by_ajax 403 (Forbidden)
send @ jquery-3.3.1.min.js:2
ajax @ jquery-3.3.1.min.js:2
add_bill_by_ajax @ custom.js:3764
(anonymous) @ bill.js:2362
dispatch @ jquery-3.3.1.min.js:2
y.handle @ jquery-3.3.1.min.js:2
Network errors网络错误
bill: "{\"customer_id\":\"1\",\"product_price_with_discount\":\"5000.00\",\"product_discount_amount\":\"0.00\",\"product_note\":\"\"}]}"
bill_id: 0
close_bill: 0
csrf_ghive: 6555e8953c1aee584cf2472c1ad14a4d
Note I have looked at other questions like this one but I have not gotten a suitable solution:请注意,我已经看过其他类似的问题,但还没有找到合适的解决方案:
POST url 403 forbidden in Codeigniter 3 POST url 403 forbidden in Codeigniter 3
Ajax request with codeigniter 403 (forbidden) Ajax 请求 codeigniter 403(禁止)
403 Forbidden Access to CodeIgniter controller from ajax request 403禁止从ajax请求访问CodeIgniter controller
etc... ETC...
Preset Apache mod_rewrite Flags modified RewriteRules causing AJAX requests to fail.The solution is either: Preset Apache mod_rewrite Flags modified RewriteRules 导致 AJAX 请求失败。解决方案是:
Deactivate mod_rewrite if you can on your hosting (not recommended)如果可以在您的主机上停用 mod_rewrite (不推荐)
Change .htaccess
of codeigniter installation from:将.htaccess
安装的 .htaccess 从:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond $1 !^(index\.php|images|robots\.txt)
to:到:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.