gpt4 book ai didi

javascript - 克雷格列表 RSS 提要

转载 作者:行者123 更新时间:2023-11-30 10:56:36 24 4
gpt4 key购买 nike

我正在尝试解析来自 craigslist rss 提要的数据。

这是提要网址 - 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>');

}
});
});

但是,我没有显示提要标题或提要的任何其他属性。如果我只是尝试将提要打印到屏幕上,我会得到“对象对象”,这意味着它正确地返回了提要。

有人知道我错过了什么吗?

最佳答案

首先:您无法从另一个域获取数据,因为 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>

关于javascript - 克雷格列表 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/695316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com