簡體   English   中英

禁止使用 codeigniter 的 Ajax 請求

[英]Ajax request with codeigniter forbidden

我正在嘗試使用 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;
    }
}

發送

$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) {
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM