【问题标题】:Ajax call request in CodeIgniter with CSRF enabled启用 CSRF 的 CodeIgniter 中的 Ajax 调用请求
【发布时间】:2015-10-26 17:31:45
【问题描述】:

我正在使用 ajax 调用通过使用 ajax 调用的数据库中的隐藏字段保存单选按钮的value,使用警报来查看它是否工作,并且确实如此。

我在 ajax 调用中提到的 URL 将其重定向到控制器,但我使用警报来查看它是否工作,但它不工作。找不到问题所在。

这里是view中的ajax调用代码:

<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>

这里是控制器:

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

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

这是模型:

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

请提出一些出路。

【问题讨论】:

  • 你的控制器里写了js代码?
  • 您的控制器中不能有alert('You Can Do It !!');。您还应该在 JS 中使用 site_url 而不是 base_url。基本 URL 将返回 CI 安装目录的路径,而不是 CI 索引文件。

标签: javascript php ajax codeigniter csrf


【解决方案1】:

您不能在控制器中使用 javascript“alert()”。 而不是“警报('你可以做到!');”使用“死('你可以做到!');”进行调试,然后签入您的控制台。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 2017-06-14
    • 1970-01-01
    • 2013-09-18
    • 2011-01-21
    • 2019-01-25
    • 1970-01-01
    • 2020-02-15
    相关资源
    最近更新 更多