Reputation: 63
I have converted xml into json and want to get some data using looping process. Actually, I want to get data in term of title,description and link,but I'm stacked here and giving error.
$(function(){
url = 'http://www.straitstimes.com/news/sport/rss.xml';
$.ajax({
type: "GET",
url: 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
dataType: 'json',
error: function(){
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function(xml){
html='';
console.log(xml.responseData.feed.entries);
for (var i = 0; i < xml.responseData.feed.entries .length; i++){
html+= xml.responseData.feed.entries[i].title+'<br/>'+
html+= xml.responseData.feed.entries[i].description+'<br/>'+
html+= xml.responseData.feed.entries[i].link+'<br/>';
}
$('#feedContainer').html(html);
}
});
});
Upvotes: 0
Views: 174
Reputation: 471
Here is the correct code JSFiddle
Some issues with your syntax on the html += lines. (The + were incorrect).
I separated out the url declarations for myself (just to make sure there was no issues).
$(function(){
var url1 = 'http://www.straitstimes.com/news/sport/rss.xml';
var url2 = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url1);
$.ajax({
type: "GET",
url: url2,
dataType: 'json',
error: function(){
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function(xml){
html='';
console.log(xml.responseData.feed.entries);
for (var i = 0; i < xml.responseData.feed.entries.length; i++) {
console.log(xml.responseData.feed.entries[i].title);
html += xml.responseData.feed.entries[i].title+'<br/>';
html += xml.responseData.feed.entries[i].description+'<br/>';
html += xml.responseData.feed.entries[i].link+'<br/>';
}
$('#feedContainer').html(html);
}
});
});
Upvotes: 1
Reputation: 108
When you use the += constructor, you're adding to an already defined variable. You therefore do not need the + at the end of the line, you need to end the statement. So
html+= xml.responseData.feed.entries[i].title+'<br/>'+
html+= xml.responseData.feed.entries[i].description+'<br/>'+
html+= xml.responseData.feed.entries[i].link+'<br/>';
should be either
html+= xml.responseData.feed.entries[i].title+'<br/>';
html+= xml.responseData.feed.entries[i].description+'<br/>;
html+= xml.responseData.feed.entries[i].link+'<br/>';
or
html = xml.responseData.feed.entries[i].title+'<br/>'+
xml.responseData.feed.entries[i].description+'<br/>'+
xml.responseData.feed.entries[i].link+'<br/>';
Upvotes: 2