我试图使用jquery.autocomplete.js进行自动完成。不幸的是,当我尝试键入要输入的内容时,调试器出现了SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符。 我仍然是codeigniter的新手,所以有人可以帮我解决这个问题吗?
Java脚本
<script type='text/javascript'>
$(function(){
$("#asisten").autocomplete({
source: "<?php echo base_url() ?>index.php/asisten/search"
});
});
</script>
控制器
public function search()
{
$keyword = $this->uri->segment(3);
$data = $this->db->from('mahasiswa')->like('nim',$keyword)->get();
foreach($data->result() as $row)
{
$arr['query'] = $keyword;
$arr['suggestions'][] = array(
'nama' =>$row->nama
);
}
echo json_encode($arr);
}
输入HTML
<input id="asisten" type="search">
JSON数据
{"query":"7","suggestions":[{"nama":"Pedro Nadirio"},{"nama":"Antony Hartono"},{"nama":"Juan"},{"nama":"Yonathan Aditya"},{"nama":"Edwin Widhiyanto"},{"nama":"Windy Hendra S"}]}
答案 0 :(得分:0)
在您的控制器中:尝试这样..
public function search()
{
$keyword = $this->uri->segment(3);
$data = $this->db->from('mahasiswa')->like('nim',$keyword)->get();
foreach($data->result_array() as $row)
{
$arr[] = $row['nama'];//array of namas
}
echo json_encode($arr);
}