2011-08-20 152 views
0

我使用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

+1

实际问题是什么? –

+0

也发布了一些你正在获得的json响应 – Rafay

+0

当netflix已经返回json时,为什么要做json_encode?我不熟悉netflix api,但是从我在你的问题中看到的。这对我来说似乎很奇怪。也许你应该通过netflix repsonse,因为它已经作为json输出了? –

回答

0

确定我想通了正确的格式,我需要发送到自动完成应对法:
视图

$("#movies").autocomplete({ 
     minLength: 2, 
     source: function(request, response) { 
      $.post("<?php echo base_url();?>welcome/search", {q: request.term}, 
       function(data){ 
        //console.log(data); 
        response(data); 

      }, 'json'); 
    }   
    }); 

控制器:

$search = $this->input->post('q'); 
     // Run some setup 
     $this->rest->initialize(array('server' => 'http://api.netflix.com/')); 
     // Pull in an array 
     $netflix_query = $this->rest->get('catalog/titles/autocomplete', array('oauth_consumer_key'=>$this->consumer_key,'term'=> $search,'output'=>'json'),'json'); 

     $json = array(); 
     foreach($netflix_query->autocomplete->autocomplete_item as $item){ 
      $temp = array("label" => $item->title->short); 
      array_push($json,$temp); 
     } 
     echo json_encode($json); 

需要的是发回查看一组对象。谢谢你们所有的答案和帮助!

0

in,因为你已经注意到了,不会做你希望与阵列喜欢什么。使用$.each

+0

我的代码 '$。每个(data.autocomplete.autocomplete_item,功能(I){ \t \t \t响应(data.autocomplete.autocomplete_item [I] .title.short ); \t \t \t})' 我仍然会得到相同的结果 –

+0

那么,它应该是'$。每个(data.autocomplete.autocomplete_item,功能(I,项目){响应(item.title.short); })',但很明显,autocomplete_item是一个字符串... – Malvolio

+0

没有骰子...它仍然循环数组值作为字符串位置并返回字符串位置。文本框将有“浪荡公子”作为搜索和自动完成读取: 小号 W¯¯ 我 ň 摹 Ë [R 小号 非常奇怪和令人沮丧的行为...... –

0

据我所知,for (property in object)意味着你想访问它的每个属性,而不是通过索引访问它们。如果你想通过索引访问它们,你可能需要使用标准的for循环。

for (i = 0; i <= 10; i++) { 
    response(data.autocomplete.autocomplete_item[i].title.short); 
} 

,或者如果你仍然想使用你的代码,试试这个:

for (i in data.autocomplete.autocomplete_item) { 
    response(i.title.short); 
} 

我还没有测试他们,但是我觉得你有这个想法。

+0

没有骰子...某处循环是返回个别元素而不是位置。由于在文本框中将是“浪荡公子”,但自动完成貌似 小号 W¯¯ 我 ň 摹 Ë [R 小号 –

相关问题