mardi 4 août 2015

Ajax call request in CodeIgniter with CSRF enabled

I am using an ajax call to save value of a radio button through hidden field in database using ajax call, used an alert to see if its working, and it is.

The URL I mentioned in the ajax call is redirecting it to a controller but I used an alert to see if its working, but its not working. Can't locate what's the issue.

Here is the code of ajax call in view:

<script type="text/javascript">
$('#save').click(function(){
    var hint=$('#hidfield').val();
    alert('Oops, your response is '+hint);

    $.ajax({
        url: '<?php echo base_url();?>welcome/save_answer',
        type: "post",
        dataType: "html",
        data:{hint:$hint, <?php echo $this->security->get_csrf_token_name();?>: "<?php echo $this->security->get_csrf_hash();?>"},
        success: function(data) {

        }
    });
});
</script>

Here is the controller:

function save_answer()
{
    alert('You Can Do It !!');
    $data = array(
        'hint'=>$this->input->post('hint')
    );

    $this->base_model->save_answer($data);
}

Here is the model:

function save_answer($data)
{       
    $this->db->insert('questions',$data);
}

Please suggest some way out.

Aucun commentaire:

Enregistrer un commentaire