duanfan1869 2015-08-04 10:40
浏览 95

启用CSRF的CodeIgniter中的Ajax调用请求

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.

  • 写回答

1条回答 默认 最新

  • douliu3831 2015-08-04 11:09
    关注

    You cannot used javascript "alert()" inside controller. instead of "alert('You Can Do It !!');" use "die('You Can Do It !!');" to debug, and check in to your console.

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献