2011-08-20 147 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 小號 –

相關問題