Codeigniter를 사용하고 jQuery 자동 완성을 사용하려고합니다. 또한 Netflix에서 자동 완성 데이터를 받기 때문에 @Phil Sturgeon 클라이언트 라이브러리를 Codeigniter에 사용하고 있습니다. 나는 올바른 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)
}
이 문자열 같은 역할을 통해 I 루프. 결과는 "스윙 어"라고하자,이 반환합니다
Object.value =의
Object.value =
Object.value W = 내가
등등.
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으로 출력 되었기 때문이다. –