2016-05-04 100 views
1

我正在嘗試使用jquery和codeigniter框架執行ajax請求,但是當我嘗試訪問服務器頁面時,我有一個禁止訪問。禁止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); 
      } 
     }); 
    }); 

錯誤警報只是顯示 「禁止」,僅此而已。

有誰知道這是什麼問題?

這裏的CONTROLER:

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; 
    } 
} 
+0

我怎麼知道它是否接受POST方法? – iAmoric

+0

我想這是相關的:http://stackoverflow.com/questions/32478355/ajax-csrf-403-forbidden-codeigniter –

+0

我可以看到控制器? –

回答

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

是的,當csrf檢查失敗時,codeigniter將警告禁止。 – wolfrevo

+0

謝謝。它解決了問題 – iAmoric

+0

不客氣。讓我知道與CI有關的任何事情。乾杯 –