使用 jQuery 阅读 RSS 提要?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11346990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:30:01  来源:igfitidea点击:

Reading RSS feed with jQuery?

jqueryjquery-pluginsrssxml-parsingrss-reader

提问by oshirowanen

Using the jQuery rss pluging jFeed, and using their example code on their website, I have created the following code which does not seem to work:

使用 jQuery rss 插件jFeed,并使用他们网站上的示例代码,我创建了以下似乎不起作用的代码:

jQuery.getFeed({
    url: 'http://www.hotukdeals.com/rss/hot',
    success: function(feed) {
        alert(feed.title);
    }
});

I get a message saying:

我收到一条消息说:

XMLHttpRequest cannot load http://www.hotukdeals.com/rss/hot. Origin http://intranet is not allowed by Access-Control-Allow-Origin.

Anyone know why I am getting this access control message? This rss feed works fine in my desktop and online rss readers...

有谁知道为什么我会收到此访问控制消息?这个 rss 提要在我的桌面和在线 rss 阅读器中运行良好...

回答by Aman Virk

WARNING

警告

The Google Feed APIis officially deprecatedand doesn't work anymore!

Google Feed API已正式弃用不再有效

It can be done very easily without a plugin and the returned data would be in json

它可以在没有插件的情况下很容易地完成,并且返回的数据将在 json 中

        $(function(){
        url = 'http://www.thetutlage.com/rss.xml';
        $.ajax({
        type: "GET",
        url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
        dataType: 'json',
        error: function(){
            alert('Unable to load feed, Incorrect path or invalid feed');
        },
        success: function(xml){
            values = xml.responseData.feed.entries;
            console.log(values);
        }
    });
    });

Just make sure it points towards an xmlfile and change the url to url Rss feed,

只需确保它指向一个xml文件并将 url 更改为 url Rss feed,

回答by Sirko

Your failing because of the same origin policyof JavaScript, which basically restricts you in the locations you can retrieve and manipulate files from.

你的失败是因为JavaScript 的同源策略,它基本上限制了你在你可以检索和操作文件的位置。

In general you can't retrieve content (in your case an rss feed) from locations different than the current page. Exceptions are just images and scripts.

通常,您无法从当前页面以外的位置检索内容(在您的情况下为 rss 提要)。例外只是图像和脚本。

So one solution in your case may be to set up a proxy script on your server, which just calls the RSS feed and relays the results to your page. That way from the browser's perspective all content comes from the same origin.

因此,在您的情况下,一种解决方案可能是在您的服务器上设置一个代理脚本,它只调用 RSS 提要并将结果中继到您的页面。这样从浏览器的角度来看,所有内容都来自同一个来源。