[英]Parsing RSS Feed with JavaScript doesn't work for all items in RSS feed
所以這是我用於訪問RSS提要的測試代碼。 此外, 這是我試圖解析的RSS提要 。 這不是項目的最終設計,只是試圖將它拼湊在一起。
它適用於我使用c.title,c.link部分,但不是 c.description或c.pubDate部分。 它只是說它是未定義的。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>not finished yet</title>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' >/*
/*
* jGFeed 1.0 - Google Feed API abstraction plugin for jQuery
*
* Copyright (c) 2009 jQuery HowTo
*
* Licensed under the GPL license:
* http://www.gnu.org/licenses/gpl.html
*
* URL:
* http://jquery-howto.blogspot.com
*
* Author URL:
* http://me.boo.uz
*
*/
(function ($) {
$.extend({
jGFeed: function (url, fnk, num, key) {
if (url == null) {
return false;
}
var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;
if (num != null) {
gurl += "&num=" + num;
}
if (key != null) {
gurl += "&key=" + key;
}
$.getJSON(gurl, function (data) {
if (typeof fnk == "function") {
fnk.call(this, data.responseData.feed);
} else {
return false;
}
});
}
});
})(jQuery);</script>
<script type='text/javascript'>
$(window).load(function () {
$.jGFeed('http://www.scarletknights.com/rss/rss.asp?sportid=1',
function (feeds) {
if (!feeds) {
alert('Trouble getting RSS feed :(');
return false;
}
for (var i = 0; i < feeds.entries.length; i++) {
var entry = feeds.entries[i];
console.log(entry);
// Entry title
$('#results').append('<h1>' + entry.title + '</h1>' + '<br/>');
}
}, 10);
});
</script>
</head>
<body>
<div id="results"></div>
</body>
</html>
您對Feed本身和Google Feed API為您提供的(已處理的)返回JSON感到困惑。 請記住,Google的Feed API不會返回與Feed格式相同的格式。
看一下您要求的JSON返回的文檔 ,您將看到entries
數組中項目的可用屬性; 它們包括title
和link
,但不包括description
或pubDate
。 content
或contentSnippet
可能是您最接近description
; publishedDate
將是feed中的pubDate
。
谷歌這樣做是因為他們的Feed API可以解析RSS而不是RSS; 它還解析Atom,並從兩種類型的feed中以一致的格式返回一個對象,以使您的生活更輕松。
雖然您使用的Feed包含title
, link
, description
和pubDate
字段,但您通過Google的API傳遞該數據。 您可以在此處查看完整文檔,但您想要的字段稱為title
, link
, contentSnippet
和publishedDate
。
以下是您的代碼的工作示例: http : //jsfiddle.net/LacE5/3/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.