Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting RSS To JSON

Tags:

json

jquery

I'm trying to convert RSS to JSON but when I use console in Chrome I get this error

XMLHttpRequest cannot load http://www.blastcasta.com/feed-to-json.aspx?feedurl=http://www.startalkradio.net/?page_id=354. Origin null is not allowed by Access-Control-Allow-Origin.

jQuery:

$(document).ready(function() {
    var rss_url = "http://www.blastcasta.com/feed-to-json.aspx?feedurl=http://www.startalkradio.net/?page_id=354";

    $.ajax({
        type : 'GET',
        url : rss_url,
        succces : function(data) {
            $.each(data.rss, function(i, item) {
                $("#results").append('<ul>' + item.channel.item.description + '</ul>');

            });
        },
        dataType : 'json',
        async : true
    });

    function fetchRss(data) {
        $.each(data.rss, function(i, item) {
            $("#results").append('<ul>' + item.channel.item.description + '</ul>');

        });
    }

});
like image 712
Antarr Byrd Avatar asked May 07 '12 20:05

Antarr Byrd


2 Answers

UPDATE: this answer suggests using the Google Feed API which has been deprecated. Other options include Superfeedr: Converting RSS to JSON

That error happens because the RSS file is not in your server, so you are violating the Access-Control-Allow-Origin.

Try this plugin that is used for cross-domain purposes with RSS:

http://jquery-howto.blogspot.com/2009/11/cross-domain-rss-to-json-converter.html

EDIT:

Library download link:

http://code.google.com/p/lomodroid/source/browse/src/functions/jquery.jgfeed.js

Live demo with twitter:

http://jsfiddle.net/oscarj24/NbDWb/

Live demo with the link used in the question:

http://jsfiddle.net/oscarj24/NbDWb/3/

This will convert RSS to JSON

<script src="jquery.js"></script>
<script src="jquery.jgfeed.js"></script>
<script>
$.jGFeed('http://twitter.com/statuses/user_timeline/26767000.rss',
  function(feeds){
    // Check for errors
    if(!feeds){
      // there was an error
      return false;
    }
    // do whatever you want with feeds here
    for(var i=0; i<feeds.entries.length; i++){
      var entry = feeds.entries[i];
      // Entry title
      entry.title;
    }
  }, 10);
</script>
like image 76
Oscar Jara Avatar answered Nov 14 '22 20:11

Oscar Jara


Well basically it is really simple. You cannot load that URL because JavaScript/your browser does not allow it. (Because of same origin policy).

You will have to do it with a scripting language (like PHP) or something, but not by JavaScript

Unless you have access to the source (RSS location) and can add headers to allow the origin

like image 33
Rene Pot Avatar answered Nov 14 '22 19:11

Rene Pot