CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request (/showthread.php?tid=79045) |
CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request - josephgodwinke - 04-12-2021 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\":\"\"}]}" RE: CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request - kleber - 04-19-2021 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. RE: CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request - josephgodwinke - 04-20-2021 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> RE: CodeIgniter 3 - 403 Forbidden on POST from jQuery.ajax() asynchronous request - kleber - 04-20-2021 This may work for you, but I would not go that way... |