CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request |
I am getting a 403 forbidden error on jQuery.ajax() post request.
[b][b][b]application/config/config.php[/b][/b][/b]
Code: $config['csrf_protection'] = TRUE; [b][b]application/views/billing.php[/b][/b]
Code: <input type="text" class="txt_csrfname" name="<?= $this->security->get_csrf_token_name(); ?>" value="<?= $this->security->get_csrf_hash(); ?>"> [b][b]assets/customers/js/bill.js[/b][/b] Code: function add_bill_by_ajax(bill_object,bill_id){ application/controllers/Bill.php Code: function add_bill_by_ajax(){ Browser Console errors Code: jquery-3.3.1.min.js:2 POST http://localhost/Bill/add_bill_by_ajax 403 (Forbidden) Browser Network errors Code: bill: "{\"customer_id\":\"1\",\"product_price_with_discount\":\"5000.00\",\"product_discount_amount\":\"0.00\",\"product_note\":\"\"}]}" $string = $this->load->view('return_view_as_data', '', TRUE);
I generally use ajaxPrefilter(https://api.jquery.com/jquery.ajaxPrefilter/) in my projects:
Code: $.ajaxPrefilter(function (options, originalOptions, jqXHR) { I just need to define the values csrf_name, csrf in the header for example, that all requests from my system are sent with this security token.
Preset Apache mod_rewrite Flags modified RewriteRules causing AJAX requests to fail.The solution is either:
1. Deactivate mod_rewrite if you can on your hosting (not recommended) 2. Change .htaccess of codeigniter installation from: Code: RewriteEngine On to: Code: <IfModule mod_rewrite.c> $string = $this->load->view('return_view_as_data', '', TRUE);
|
Welcome Guest, Not a member yet? Register Sign In |