craigslist rss feed

痴心易碎 提交于 2019-11-28 02:19:46

问题


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?


回答1:


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>



回答2:


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/



来源:https://stackoverflow.com/questions/695316/craigslist-rss-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!