POST url 403 forbidden in Codeigniter 3
hey guys just found the answer. Thank you for all your suggestions and advice.
<script type="text/javascript">function q_header() { var question = document.getElementById('question').value; $.ajax({ url: "<?php echo base_url('Mainx/insert_question_header');?>", type: "post", data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>',"question":question}, success: function(){ alert("success"); }, error:function(){ alert("failure"); } });}</script>
I try this code and instead using question_header i change it to question. This also work even when you set your $config[csrf_protection]=TRUE; or $config[csrf_generate]=TRUE;this is my updated answer.
Try it with this:
$.ajax({ url : "<?php echo base_url('Mainx/insert_question_header');?>", crossDomain: true, contentType: "application/x-www-form-urlencoded", method: "POST", data: { name: 'question_header', '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>' }, dataType: 'json', success: function(data){ //alert('data save'); }, error: function (jqXHR, textStatus, errorThrown){ alert('Error adding'); } });
So you have to place the CSRF token to the data:
section.