Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

javascript - craigslist rss feed

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...