Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an RSS feed using JavaScript (External Domain)?

Question

I need to parse an RSS feed and display the parsed details in an HTML page.

Solution I Found

How to parse an RSS feed using JavaScript? is a very similar question and I followed it.

Using above question, I build the following code.

 <script>
  $(document).ready(function() {
    //feed to parse
    var feed = "https://feeds.feedburner.com/raymondcamdensblog?format=xml";

    $.ajax(feed, {
        accepts:{
            xml:"application/rss+xml"
        },
        dataType:"xml",
        success:function(data) {
            //Credit: http://stackoverflow.com/questions/10943544/how-to-parse-an-rss-feed-using-javascript

            $(data).find("item").each(function () { // or "item" or whatever suits your feed
                var el = $(this);
                document.write("------------------------");
                document.write("title      : " + el.find("title").text());
                document.write("link       : " + el.find("link").text());
                document.write("description: " + el.find("description").text());
            });


        }   
    });

});
</script>

The Error

Failed to load https://feeds.feedburner.com/raymondcamdensblog?format=xml: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

What I need

How can I change my code to read RSS feeds using JavaScript without getting above error?

like image 335
I am the Most Stupid Person Avatar asked Aug 02 '18 07:08

I am the Most Stupid Person


People also ask

How do I read an RSS feed from a website?

You can try finding an RSS feed by checking a web page's source code. Don't panic! It's easier than it sounds. Right click an empty space on the website you'd like an RSS feed for, then click View Page Source (the exact wording may vary depending on your browser).

How do I display an RSS feed in HTML?

How To Display RSS Feed In HTML Website? Step 1: Select HTML as your website platform from the options. Step 2: Set the width and height (or auto) of your feed and click on “Get Code”. Step 3: Copy the given Code and paste it into the back-end of any webpage where you want to display RSS feed.


1 Answers

You could use something like https://rss2json.com. It parses the feed to json for javascript:

var feedURL = "https://feeds.feedburner.com/raymondcamdensblog?format=xml";
$.ajax({
  type: 'GET',
  url: "https://api.rss2json.com/v1/api.json?rss_url=" + feedURL,
  dataType: 'jsonp',
  success: function(result) {
    console.log(result);
  }
});
like image 98
Saeed Avatar answered Sep 20 '22 21:09

Saeed