我正在尝试解析来自craigslist rss提要的数据。
这是提要url - http://www.craigslist.org/about/best/all/index.rss
我正在使用jfeed,我的代码如下
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>');
}
});
});但是,我不能显示提要标题或提要的任何其他属性。如果我只是尝试将提要打印到屏幕上,我会得到'Object Object‘,这意味着它正确地返回了提要。
有人知道我错过了什么吗?
发布于 2011-08-16 00:44:42
第一:您不能从其他域获取数据,因为crossdomain策略。我不知道jfeed,但在我的项目中,我想出了这个解决方案。使用这个简单的函数,您可以节省一些带宽和代码开销。
工作示例
http://intervisual.de/stackoverflow/fetchxml/index.html
proxy.php (源: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>发布于 2012-01-27 16:27:16
或者,您可以使用其他一些服务来读取RSS提要并将其转换为JSON,如果您不能访问任何服务器端环境,这将非常有用。
为此,我通常使用YQL,但肯定还有其他服务。
下面是一个使用craigslist的工作示例,源代码是:http://jsfiddle.net/soparrissays/NFSaq/2/
https://stackoverflow.com/questions/695316
复制相似问题