我正在尝试使用Google feed Api在一个网站上加载多个提要。我有问题加载图像到我的馈送,无法找到解决方案。有谁能帮我吗?下面是一个codepen。
提要有这样的标记:
<img>http://img.ephoto.sk/images/content/articles/c285367e4e39cfa8056f2c95ec715f76c1e758af.jpg</img>JS代码:
function parseFeed(url, container) {
$.ajax({
url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&callback=?&q=' + encodeURIComponent(url), // "num=5" will show 5 articles
dataType: 'json',
success: function (data) {
// log object data in console
console.log(data.responseData.feed);
// for each entry... *
$.each(data.responseData.feed.entries, function (key, value) {
var title = '<li><a class="title" href="' + value.link + '" target="_blank">' + value.title + '</a></li>';
var image = '<img class="thumbnail" src="' + value.img + '">';
var entry = '<div class="entry"><ul>' + image + title + '</ul></div>';
// * append entire entry in container
$(container).append(entry);
});
},
// if there's an error... *
error: function (errorThrown) {
// * log error message in console
console.log(errorThrown);
// * show error message
alert('Niekde vo feede sa vyskytla chyba.');
}
});
}
$(document).ready(function () {
parseFeed('http://feeds.feedburner.com/FotoportlEphotosk-FotoFotografieFotoaparty', '#ephoto');
});Codepen:http://codepen.io/MichalSK/pen/rVEwPy
有没有什么解决方案可以让这段代码与这个标记一起工作?
<image>
<url>http://...</url>
<title>Lorem ipsum</title>
<pubDate>Wed, 19 Aug 2015 13:00:00 +0200</pubDate>
<link>http://...</link>
</image>我希望这也能帮助其他希望使用Google Feed Api的人。
发布于 2015-08-21 00:36:07
您确定data.responseData.feed.entries参数中有img参数吗?您的value.link正在从data.responseData.feed.entries的参数中获取链接参数值,但您没有"img“参数来获取其值
var image = '<img class="thumbnail" src="' + value.img + '">';
因此在获取未定义参数之前,您需要将图像链接存储在正在加载的data对象中。
下面的代码显示了图像,因为有为这些图像定义的链接。在data.responseData.feed.entries.value中,没有用于通过其值获取链接的img参数
function parseFeed(url, container) {
$.ajax({
url: document.location.protocol
+ '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&callback=?&q='
+ encodeURIComponent(url), // "num=5" will show 5 articles
dataType: 'json',
success: function (data) {
// log object data in console
console.log(data.responseData.feed);
// for each entry... *
$.each(data.responseData.feed.entries, function (key, value) {
//valid link to image source
var img = "http://rocketdock.com/images/screenshots/Debian-Logo.png"
var title = '<li><a class="title" href="' + value.link
+ '" target="_blank">' + value.title + '</a></li>';
var image = '<img class="thumbnail" src="' + img + '">';
var entry = '<div class="entry"><ul>' + image + title + '</ul></div>';
// * append entire entry in container
$(container).append(entry);
});
},
// if there's an error... *
error: function (errorThrown) {
// * log error message in console
console.log(errorThrown);
// * show error message
alert('Niekde vo feede sa vyskytla chyba.');
}
});
}
$(document).ready(function () {
parseFeed('http://feeds.feedburner.com/FotoportlEphotosk-FotoFotografieFotoaparty',
'#ephoto');
});html,body {
background-color:#222;
color:#fff;
font-family:"Noto Sans", sans-serif;
font-size:1em;
font-weight:400;
line-height:1.45;
}
a{
color: #fff;
text-decoration: none;
}
img{
width:50px;
height:50px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div id="container">
<div id="ephoto"></div>
</div>
从控制台检查你的数据,你找不到img参数。
https://stackoverflow.com/questions/32123250
复制相似问题