2013-07-09 58 views
0

我有一個腳本,如果我使用orderby=published不起作用,但如果使用orderby=viewcountorderby=rating,則可以工作。查詢有問題嗎?或者發佈返回不同於此腳本的json結果。這個腳本是從
youtube-ajax-search-apiYouTube的搜索結果適用於orderby = viewCount而不是orderby =已發佈

query="http://gdata.youtube.com/feeds/mobile/videos?alt=json-in-script&q=talking+tom&orderby=viewCount&start-index=1&max-results=20"; 

query="http://gdata.youtube.com/feeds/mobile/videos?alt=json-in-script&q=talking+tom&orderby=published&start-index=1&max-results=20"; 


<script type="text/javascript" src="jquery-1.6.2.min.js"></script> 
<script type="text/javascript"> 
function SearchYouTube(query) { 
    $.ajax({ 
     url: 'http://gdata.youtube.com/feeds/mobile/videos?alt=json-in-script&q=' + query, 
     dataType: 'jsonp', 
     success: function (data) { 
      var row = ""; 
      for (i = 0; i < data.feed.entry.length; i++) { 
       row += "<div class='search_item'>"; 
       row += "<table width='100%'>"; 
       row += "<tr>"; 
       row += "<td vAlign='top' align='left'>"; 
       row += "<a href='#' ><img width='120px' height='80px' src=" + data.feed.entry[i].media$group.media$thumbnail[0].url + " /></a>"; 
       row += "</td>"; 
       row += "<td vAlign='top' width='100%' align='left'>"; 
       row += "<a href='#' ><b>" + data.feed.entry[i].media$group.media$title.$t + "</b></a><br/>"; 
       row += "<span style='font-size:12px; color:#555555'>by " + data.feed.entry[i].author[0].name.$t + "</span><br/>"; 
       row += "<span style='font-size:12px' color:#666666>" + data.feed.entry[i].yt$statistics.viewCount + " views" + "<span><br/>"; 
       row += "</td>"; 
       row += "</tr>"; 
       row += "</table>"; 
       row += "</div>"; 
      } 
      document.getElementById("search-results-block").innerHTML = row; 
     }, 
     error: function() { 
      alert("Error loading youtube video results"); 
     } 
    }); 
    return false; 
} 
</script> 

回答

0

看來,也許你正在使用錯誤的URL。從你的代碼:

query="http://gdata.youtube.com/feeds/mobile/videos?alt=json-in-script&q=talking+tom&orderby=published&start-index=1&max-results=20"; 
... 
function SearchYouTube(query) { 
    ... 
    url: 'http://gdata.youtube.com/feeds/mobile/videos?alt=json-in-script&q=' + query 
    ... 

你需要傳遞給你的函數SearchYouTube(query)查詢字符串沒有http-等等,誇誇其談前綴。不是你在你上面定義的query變量中定義的,而是像這樣的:

query="talking+tom&orderby=published&start-index=1&max-results=20";