Javascript 在JQuery中使用CORS获取RSS文件
我可能做错了。。。我觉得我好像在用代码挥舞 我从这个开始:Javascript 在JQuery中使用CORS获取RSS文件,javascript,jquery,xml,cors,Javascript,Jquery,Xml,Cors,我可能做错了。。。我觉得我好像在用代码挥舞 我从这个开始: function getData(){ //getting XML data $.get('http://rss.cnn.com/rss/edition.rss', function(d){ //need to capture the data to process the data. //we are representing the data with the letter d console.log(d
function getData(){
//getting XML data
$.get('http://rss.cnn.com/rss/edition.rss', function(d){
//need to capture the data to process the data.
//we are representing the data with the letter d
console.log(d); //this method is good to see the structure of the data if you don't know it already
$(d).find('item').each( function(){
//find each item and do the following:
var article = $(this);
var title = article.find('title').text();
var description = article.find('description').text();
var guid = article.find('guid').text();
var date = article.find('pubDate').text();
var theText = '<article><h3><a href="'+guid+'">'+title+'</a></h3><p>Published on:'+date+'</p><p>'+description+'</p>';
$('#content').append(theText);
}); //end each
}) //end anon func
}//end getData();
getData();
下面是我现在收到的错误消息:
对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头
我问这个问题可能看起来很傻,但请求的资源是我想要从中获取数据的rss提要吗?或者是我的文件。我读了太多关于这个的书,我的头和眼睛都因为困惑而打转。请帮忙
谢谢您所请求的资源(rss文件)位于CNN的服务器中。但是,它们不允许您从浏览器访问。这就是为什么你会犯CORS错误。有不同的方法来克服这个问题,比如使用JSONP。然而,CNN仍然必须在其服务器中启用jsonp 您唯一能做的就是在服务器端启用
accesscontrolalloworigin
,然后请求
var requestOptions: RequestInit = {
method: 'GET',
redirect: 'follow'
};
const CORS_PROXY = "https://cors-anywhere.herokuapp.com/"
fetch(CORS_PROXY + "http://fetchrss.com/rss/5f56c6803c140278522985135f56c72f61579219a908f6e2.json", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => console.log('error', error));
四年后更新,所以如果不符合上下文,请随意删除。尝试添加CORS_代理位,因为它对我有效。我在rss解析npm模块文档中找到了修复程序,这意味着CNN不允许
http://rss.cnn.com/rss/edition.rss
将使用客户端JavaScript从其自己的域以外的任何域获取。他们必须添加访问控制允许来源
标题;除了通过服务器而不是直接在浏览器中请求之外,您没有什么可以做的。
var requestOptions: RequestInit = {
method: 'GET',
redirect: 'follow'
};
const CORS_PROXY = "https://cors-anywhere.herokuapp.com/"
fetch(CORS_PROXY + "http://fetchrss.com/rss/5f56c6803c140278522985135f56c72f61579219a908f6e2.json", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => console.log('error', error));