我使用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輸出了? –