Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

onchange using ajax, codeigniter, dropdown, Enable CSRF Token

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...