Zeratas
Zeratas

Reputation: 1015

Parsing RSS Feed with JavaScript doesn't work for all items in RSS feed

So this is my testing code for accessing the RSS feed. Also, here is the RSS feed I am trying to parse. This isn't the final design of the project, just trying to piece it together.

It works when I use the c.title,c.link parts, but NOT the c.description or c.pubDate part. It just says it is undefined.

    <!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title>not finished yet</title>

      <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>


          <script type='text/javascript' >/*
     /*
      * jGFeed 1.0 - Google Feed API abstraction plugin for jQuery
      *
      * Copyright (c) 2009 jQuery HowTo
      *
      * Licensed under the GPL license:
      *   http://www.gnu.org/licenses/gpl.html
      *
      * URL:
      *   http://jquery-howto.blogspot.com
      *
      * Author URL:
      *   http://me.boo.uz
      *
      */
     (function ($) {
         $.extend({
             jGFeed: function (url, fnk, num, key) {
                 if (url == null) {
                     return false;
                 }
                 var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;
                 if (num != null) {
                     gurl += "&num=" + num;
                 }
                 if (key != null) {
                     gurl += "&key=" + key;
                 }
                 $.getJSON(gurl, function (data) {
                     if (typeof fnk == "function") {
                         fnk.call(this, data.responseData.feed);
                     } else {
                         return false;
                     }
                 });
             }
         });
     })(jQuery);</script>




<script type='text/javascript'>

    $(window).load(function () {
        $.jGFeed('http://www.scarletknights.com/rss/rss.asp?sportid=1',
            function (feeds) {
                if (!feeds) {
                    alert('Trouble getting RSS feed :(');
                    return false;
                }
                for (var i = 0; i < feeds.entries.length; i++) {
                    var entry = feeds.entries[i];
                    console.log(entry);
                    // Entry title
                    $('#results').append('<h1>' + entry.title + '</h1>' + '<br/>');
                }
            }, 10);

    });
</script>

    </head>
    <body>
      <div id="results"></div>


    </body>


    </html>

Upvotes: 0

Views: 1482

Answers (2)

Anton Sarukhanov
Anton Sarukhanov

Reputation: 26

While the feed you're using has fields called title, link, description, and pubDate, you're passing that data through Google's API. You can look at the full documentation here, but the fields you want are called title, link, contentSnippet, and publishedDate.

Here's a working example of your code: http://jsfiddle.net/LacE5/3/

Upvotes: 1

Matt Gibson
Matt Gibson

Reputation: 38238

You're getting confused between the feed itself and the (processed) return JSON that Google's Feed API gives you. Bear in mind that Google's Feed API doesn't return a format that's identical to the feed format.

Take a look at the documentation for the JSON return you're requesting, and you'll see the available properties for the items in the entries array; they include a title and a link, but not a description or a pubDate. content or contentSnippet is probably the closest you'll get to description; publishedDate will be the pubDate from the feed.

Google does this because their Feed API can parse more than RSS; it also parses Atom, and returns an object in a consistent format from both types of feed, to make life easier for you.

Upvotes: 1

Related Questions