[英]Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin
[英]Why do I keep getting codeigniter “is not allowed by Access-Control-Allow-Origin” error with AJAX request
我的代碼始終出現以下問題。
我有一個具有以下功能的數據模型:
data_model.php
public function testFunc(){
return "string";
}
site.php (控制器)
public function get_more_data(){
$this->load->model('data_model');
$data['test']=$this->data_model->testFunc();
return $data;
}
home.php (查看)
<html>
<body>
<button class="test">test</button>
</body>
<script>
$('.test').click(function(){
$id=1;
$.ajax
({
url: '<?php echo site_url() ?>index/site/get_more_data',
data: $id,
type: 'post',
success: function(result)
{
alert(result);
}
});
});
</html>
但是,每次我單擊按鈕時,我總是會收到此錯誤:
XMLHttpRequest cannot load http://[::1]/codeignitor/index.phpindex/site/get_more_data. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
我在做什么錯或在這里沒有正確理解?
謝謝,
我通過以下過程在CodeIgniter項目中使用ajax:
我使用查詢將數據發送到控制器(例如:xxxx.com?id = 5)
我在控制器中使用echo接收數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.