I want to generate a caste dropdown based on religion, Below is my Ajax Code:
<script type="text/javascript">
//Generating CSRF Token
var CFG = {
url: '<?php echo $this->config->item('base_url');?>',
csrf_test_name: '<?php echo $this->security->get_csrf_hash();?>'
};
$(document).ready(function($){
//Append to Ajax
$.ajaxSetup({data: {token: CFG.csrf_test_name}});
$(document).ajaxSuccess(function(e,x) {
var result = $.parseJSON(x.responseText);
$('input:hidden[name="token"]').val(result.token);
$.ajaxSetup({data: {token: result.token}});
});
// Dropdown Religion on change
$('#religion_id').change(function(){
var religion_id = {
religion_id:$('#religion_id option:selected').val()
};
console.log(religion_id);
$.ajax({
type: "POST",
url: "<?php echo site_url('policy/GetCaste'); ?>",
data:religion_id,
dataType:"json",//return type expected as json
success: function(CasteRes){
$.each(CasteRes,function(key,val){
var opt = $('<option />');
opt.val(key);
opt.text(val);
$('#caste_id').append(opt);
});
},
});
});
});
</script>
The issue is that 403 Forbidden Access to CodeIgniter controller from ajax request. in my config.php file I changed :
$config['csrf_token_name'] = 'csrf_test_name';
How to solve this issue? Thanks in Advance.
question from:
https://stackoverflow.com/questions/66057042/onchange-using-ajax-codeigniter-dropdown-enable-csrf-token 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…