这是视图中的ajax函数
<script type="text/javascript">
$('#name').on('change',function(){
var uid = document.getElementById('name').value;
$.ajax({
url : '<?php echo base_url('index.php/Manager_Settings_Controller/getUsername');?>',
method : 'get',
data : {'uid' : uid },
success : function(result){
console.log(result);
}
});
});
这是控制器的getUsername函数
public function getUsername(){
$uid = $this->input->get('uid');
$this->load->model('Manager_Settings_Model');
$data = $this->Manager_Settings_Model->getUsername($uid);
return $data['username'];
}
这是模型功能
function getUsername($uid){
$this->db->select('username');
$query = $this->db->get_where('user',array('u_id'=>$uid));
foreach($query -> result() as $row){
$data = array(
'username' => $row->username,
);
}
return $data;
}
如果有人可以帮助我真的很棒。谢谢inadvance
答案 0 :(得分:1)
public function getUsername(){
$uid = $this->input->get('uid');
$this->load->model('Manager_Settings_Model');
$data = $this->Manager_Settings_Model->getUsername($uid);
echo $data['username'];
}
你必须在控制器中回显不返回,所以你在ajax的成功函数的console.log中得到错误或结果。