【发布时间】:2014-01-15 08:07:05
【问题描述】:
嗨,我正在使用带有 codeigniter 的 jquery 自动完成,我的 reslut 数组来自数据库,我正在使用 json_encode() 发回数组。但作为回应,我无法在搜索中显示值但我得到空结果,这意味着我得到值但我无法显示但值即将到来我可以看到空的 lis,我的数据数组是这样的
[{"countryid":"1","countryname":"Afghanistan"},{"countryid":"2","countryname":"Albania"},{"countryid":"3","countryname":"Algeria"},{"countryid":"4","countryname":"American Samoa"},{"countryid":"5","countryname":"Andorra"}]
我的模型函数是这样的
public function search_countries($string)
{
$this->db->like('countryname', $string);
$query = $this->db->get('countries');
return $query->result_array();
}
我的控制器功能是这样的
public function search_countries()
{
$string = $this->input->post('countryname');
$data = $this->user_m->search_countries($string);
$this->output->set_header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
}
这里是jquery函数
$( ".country" ).autocomplete({
source: function( request, response ) {
$.post("signup/search_countries", {countryname: request.term}, function(data){
response(data, function( data ) {
return {
label: data.countryid,
value: data.countryname
}
});
});
}
});
【问题讨论】:
-
您需要使用正确的键,如返回 json。
-
键是正确的@cubuzoa
-
你也检查过你在萤火虫上的响应数据吗?它是否正确并且您没有收到任何 js 错误?
标签: javascript php jquery arrays codeigniter