我正在使用Codeigniter版本3+,Jquery版本3+。我試圖通過ajax請求獲取數據,但它不返回任何內容。當我檢查並看到它的請求url是錯誤的,但沒有得到我如何修改。Codeigniter Ajax請求URL問題
Ajax請求
var site_url = '<?=base_url()?>';
var id = $(this).find("option:selected").attr('value');
$.ajax({
type : 'POST',
dataType : 'json',
url: '<?=base_url()?>'+'index.php/talika_12/get_data_by_id_ajax',
data: {user_id:id},
success: function(data) {
alert(data);
$('#inst_name').text(data.talika_12_user_name);
$('#inst_account_no').text(data.talika_12_user_account_no);
}
});
控制器
public function get_data_by_id_ajax(){
$user_id = $_POST['user_id'];
$data = $this->talika_12_m->get_data_by_id($user_id);
$ajax_response_data = array(
'talika_12_user_name' => $data[0]->talika_12_user_name ,
'talika_12_user_account_no' => $data[0]->talika_12_user_account_no ,
);
echo json_encode($ajax_response_data);
}
模型
public function get_data_by_id($id){
$where_clause = array('talika_12_user_id' => $id);
$this->db->limit(1);
$val = $this->db->get_where('table_12', $where_clause)->result();
return $val;
}
Get請求的URL是(請求URL:http://localhost/test/codeIgniter/talika_12/%3C?=base_url()?%3Eindex.php/talika_12/get_data_by_id_ajax )
的問題是在你的ajax的網址 –
你可以嘗試像這樣的網址:「 <?= base_url('index.php/talika_12/get_data_by_id_ajax')?>「 –
爲什麼不簡化爲'url:」<?php echo base_url('index.php/talika_12/get_data_by_id_ajax'); ?>「'? –