我NW.JS里面,我需要能够得到RSS源,但一些XML提要,并不是所有目前只能检索数据。我想我的问题可能是CORS。我在阿贾克斯试图“设置页眉”,但nw.js浏览器拒绝对其进行设置。
我已经尝试过阿贾克斯这样的...
var cleanURL = "http://feeds.bbci.co.uk/news/uk/rss.xml"
$(document).ready(function() {
//feed to parse
var feed = cleanURL;
$.ajax(feed, {
accepts:{
xml:"application/rss+xml"
},
dataType:"xml",
success:function(data) {
//Credit: http://stackoverflow.com/questions/10943544/how-to-parse-an-rss-feed-using-javascript
$(data).find("item").each(function () { // or "item" or whatever suits your feed
var el = $(this);
console.log("------------------------");
console.log("title : " + el.find("title").text());
console.log("link : " + el.find("link").text());
console.log("description: " + el.find("description").text());
});
}
});
});
这一点也适用于像http://feeds.bbci.co.uk/news/uk/rss.xml网站,但是当我使用像提要的站点://feeds.skynews.com/feeds/rss/uk.xml我得到..even :: FAILED_TO_LOAD_RESOURCE如果我删除“饲料://”。
我在阿贾克斯尝试设置页眉
headers: {"Origin": "*"}
但我不知道这是如何工作。
这是一个CORS问题?如果是我怎么能在NW.JS克服这个
我也试着取()具有相同的结果,服务器拒绝给我的数据。
我想能够检索任何RSS Feed中的任何XML数据,而不必使用API的像rss2json等来做到这一点。
所以,事实证明,欺骗用户代理到Chrome浏览器(即使NW.JS是建立在浏览器!),使得它的工作。我用“请求”要做到这一点。
var request = require('request');
var options = {
url: cleanURL,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response.statusCode);
console.log(body);
}
}
request(options, callback);