解析多个原子提要会引发错误
Parsing multiple atom feeds throws error
我有这个脚本,它会解析多个提要,并根据脚本中有多少提要,在 id 为 content_1
、content_2
等的 div 中呈现项目。
但是,该脚本只显示其中一个供稿,我不断收到错误消息:"Uncaught TypeError: Cannot read property '1' of 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>');
谁能看出是什么问题?
试试这个..
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();
我有这个脚本,它会解析多个提要,并根据脚本中有多少提要,在 id 为 content_1
、content_2
等的 div 中呈现项目。
但是,该脚本只显示其中一个供稿,我不断收到错误消息:"Uncaught TypeError: Cannot read property '1' of 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>');
谁能看出是什么问题?
试试这个..
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();