1

I'm trying to parse the data from a craigslist rss feed.

This is the feed url - http://www.craigslist.org/about/best/all/index.rss

I'm using jfeed and my code is given below

jQuery(function() {

    jQuery.getFeed({
        url: 'proxy.php?url=http://www.craigslist.org/about/best/all/index.rss',
        success: function(feed) {        
            jQuery('#result').append('<h2>'
            + feed.title
            + '</h2>');                                

        }    
    });
});

However, I don't get the feed title displayed or any other property of the feed. If i just try to print out the feed to the screen, I get 'Object Object' which means it correctly returned the feed.

Anybody know what I am missing?

Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
  • What happens when you use "console.log" to log the object to Firebug? – bhollis Mar 29 '09 at 23:08
  • i checked and it shows the description, title, link as being empty. If i open the craigslist url directly using a web browser and view the page source, i can see values for the title, link, description, etc –  Mar 30 '09 at 02:43
  • jfeed's example-proxy.html is working here w/ http://www.craigslist.org/about/best/all/index.rss . takes a while to load, but then there it is, with title and all other properties. – ax. Mar 30 '09 at 11:59
  • When you visit "yoursite.com/proxy.php?url=http://www.craigslist.org/about/best/all/index.rss" do you see the same/correct output as when visiting the actual feed on CL? – anonymous coward May 13 '09 at 21:13

2 Answers2

3

First: You can't fetch data from another domain because the crossdomain policy. I don't know about jfeed but in my projects i came up with this Solution. With this simple function you can save some bandwidth and code overhead.

Working Example

http://intervisual.de/stackoverflow/fetchxml/index.html

proxy.php (src: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html)

<?php
// Set your return content type
header('Content-type: application/xml');

// Website url to open
$daurl = 'http://www.craigslist.org/about/best/all/index.rss';

// Get that website's content
$handle = fopen($daurl, "r");

// If there is something, read and return
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>

jQuery

$.ajax({
    type: "GET",
    url: "proxy.php",
    dataType: "xml",
    success: parseXml
 });

function parseXml(xml) {
    console.log(xml);
    $(xml).find("item").each(function() {
        var content = $(this).find("title").text()
        $("#news_list").append('<li>' + content +'</li>');
    });
}

HTML

<div id="news_list"></div>
gearsdigital
  • 13,915
  • 6
  • 44
  • 73
1

Alternatively you could use some other service to read the RSS feed and convert it to JSON, which is extremely useful if you don't have access to any server side environment.

To do this, I usually use YQL, but there are definitely other services out there.

Here is a working example using craigslist with the source: http://jsfiddle.net/soparrissays/NFSaq/2/

Parris
  • 17,833
  • 17
  • 90
  • 133