如果我使用orderby=published
,我的下面的脚本无效,但如果我使用orderby=viewcount
或orderby=rating
则会有效。查询是否有问题,或者发布是否返回不适用于此脚本的不同json
结果。这个脚本来自于
youtube-ajax-search-api
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 :(得分:0)
似乎你可能正在使用错误的网址。从你的代码:
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
...
您需要将没有http-blah-blah-blah前缀的查询字符串传递给函数SearchYouTube(query)
。不是您在上面的query
变量中定义的内容,而是类似的内容:
query="talking+tom&orderby=published&start-index=1&max-results=20";