我使用Codeigniter并尝试使用jQuery自动完成功能。我还使用@Phil Sturgeon客户端库文件用于Codeigniter,因为我从netflix获取自动完成数据。我返回正确的JSON,我可以用JSON作为单独的字符串返回而不是对象
response(data.autocomplete.autocomplete_item[0].title.short);
访问的第一个元素,但是当我遍历结果
for (var i in data.autocomplete.autocomplete_item) {
response(data.autocomplete.autocomplete_item[i].title.short)
}
它就像一个字符串。比方说,结果是 “浪荡公子”,则回复:
Object.value = S
Object.value = W
Object.value =我
等。
的JS:
$("#movies").autocomplete({
source: function(request, response) {
$.ajax({
url: "<?php echo site_url();?>/welcome/search",
dataType: "JSON",
type:"POST",
data: {
q: request.term
},
success: function(data) {
for (var i in data.autocomplete.autocomplete_item) {
response(data.autocomplete.autocomplete_item[i].title.short);
}
}
});
}
}).data("autocomplete")._renderItem = function(ul, item) {
//console.log(item);
$(ul).attr('id', 'search-autocomplete');
return $("<li class=\""+item.type+"\"></li>").data("item.autocomplete", item).append("<a href=\""+item.url+"\">"+item.title+"</a>").appendTo(ul);
};
控制器:
public function search(){
$search = $this->input->post('q');
// Run some setup
$this->rest->initialize(array('server' => 'http://api.netflix.com/'));
// set var equal to results
$netflix_query = $this->rest->get('catalog/titles/autocomplete', array('oauth_consumer_key'=>$this->consumer_key,'term'=> $search,'output'=>'json'));
//$this->rest->debug();
//$json_data = $this->load->view('nice',$data,true);
//return $json_data;
echo json_encode($netflix_query);
}
json的回报
{"autocomplete":
{"autocomplete_item":[
{"title":{"short":"The Strawberry Shortcake Movie: Sky's the Limit"}},
{"title":{"short":"Futurama the Movie: Bender's Big Score"}},
{"title":{"short":"Daffy Duck's Movie: Fantastic Island"}}
...
什么想法? 谢谢。
有一些控制台日志与回报
the url
实际问题是什么? –
也发布了一些你正在获得的json响应 – Rafay
当netflix已经返回json时,为什么要做json_encode?我不熟悉netflix api,但是从我在你的问题中看到的。这对我来说似乎很奇怪。也许你应该通过netflix repsonse,因为它已经作为json输出了? –