解析多个原子Feed会引发错误

时间:2016-06-02 12:26:04

标签: jquery atom-feed

我有这个脚本,它会解析多个Feed并使用id content_1content_2等呈现div中的项目,具体取决于脚本中有多少Feed。

请参阅jsfiddle here

但是,脚本只显示其中一个提要,并且我不断收到错误:“Uncaught TypeError:无法读取属性'1'的null”指向这行代码:

$("#content_" + idno + " ul").append('<li><img src="' + img[1] + '"><a href="' + value.link + '" target="_blank">' + value.title + '</a><div class="small">' + pubDate + '</div><div class="description">' + value.contentSnippet + '</div></li>');

任何能看出问题所在的人都会?

1 个答案:

答案 0 :(得分:0)

试试这个..

    function GetFeeds() {
  var urls = ['http://www.futurity.org/feed/','https://theconversation.com/articles.atom'];
  urls.forEach(function(Query) {
    $.ajax({
      type: "GET",
      url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(Query),
      dataType: 'json',
      error: function() {
        console.log('Unable to load feed, Incorrect path or invalid feed');
      },
      success: function(xml) {
        $(".spinner").hide();
        var idno = parseInt(urls.indexOf(Query)) + 1;
        console.log('content_' + idno);
        console.log(xml.responseData.feed.entries);
        $.each(xml.responseData.feed.entries, function(idx, value) {
          var pubDate = value.publishedDate;
          var contentImg = value.content;
          var getImgSrc = /<img[^>]+src="([^">]+)"/;
          var img = getImgSrc.exec(contentImg);
          var firstImg = img.length ? img[1] : '';
          $("#content_" + idno + " ul").append('<li><img src="' + firstImg + '"><a href="' + value.link + '" target="_blank">' + value.title + '</a><div class="small">' + pubDate + '</div><div class="description">' + value.contentSnippet + '</div></li>');
        });
      }
    });
  });
}
GetFeeds();