craigslist rss feed [英] craigslist rss feed
问题描述
我正在尝试解析craigslist rss feed中的数据。
I'm trying to parse the data from a craigslist rss feed.
这是供稿网址 - http://www.craigslist.org/about/best/all/index.rss
This is the feed url - http://www.craigslist.org/about/best/all/index.rss
我正在使用jfeed而我的代码是如下所示
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>');
}
});
});
但是,我没有显示Feed标题或Feed的任何其他属性。如果我只是尝试将输出打印到屏幕上,我会得到对象对象,这意味着它正确地返回了提要。
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?
推荐答案
首先:你无法从其他域获取数据,因为跨域策略。我不知道jfeed,但在我的项目中,我想出了这个解决方案。使用这个简单的功能,您可以节省一些带宽和代码开销。
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.
工作示例
http://intervisual.de/stackoverflow/fetchxml/index.html
proxy.php(src: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.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>
这篇关于craigslist rss feed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!