【问题标题】:Ajax request with codeigniter forbidden禁止使用 codeigniter 的 Ajax 请求
【发布时间】:2016-08-29 15:14:57
【问题描述】:

我正在尝试使用 jquery 和 codeigniter 框架执行 ajax 请求,但是当我尝试访问服务器页面时,我有一个被禁止的访问权限。

这是我的 jquery 代码:

$('#btHello').click(function () {
    var name = $('#name').val();
        alert(name);
        $.ajax({
            type:'POST',
            data:'name='+ name,
            url:'<?php echo base_url();?>index.php/AjaxTest/ajaxtest',
            success: function(result, status){
                $('#result1').html(result);
            },

            error: function (result, status, error) {
                alert(error);
            }
        });
    });

错误提示只显示“禁止”,仅此而已。

有谁知道问题出在哪里?

这是控制器:

class AjaxTest extends CI_Controller {

    public function index() {
        $this->load->view('home/head');
        $this->load->view('home/nav');
        $this->load->view('test/ajaxTest');
        $this->load->view('home/foot');
    }

    public function ajaxTest(){
        $name = $this->input->post('name');
        echo 'Hello '.$name;
    }
}

【问题讨论】:

标签: php jquery ajax codeigniter


【解决方案1】:

发送

$this->security->get_csrf_token_name() : $this->security->get_csrf_hash()

请求。希望能解决你的问题

$.ajax({
    url: "<?php echo site_url().'/admin/getNewLocations' ?>",
    type: "POST",
    data: {
        func: 'getNewLocations',
        '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
    },
    success: function(data) {
    }
});

【讨论】:

  • 是的,当 csrf 检查失败时,codeigniter 会警告禁止。
  • 不客气。让我知道与 CI 相关的任何事情。干杯
猜你喜欢
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多