在javascript(跨域)中读取RSS XML

时间:2022-08-23 10:02:47

I want to read rss(xml) file but without using google rss feed.
i have try jsonp but it download the file and it throw a error "Uncaught SyntaxError: Unexpected token < "

我想阅读rss(xml)文件,但不使用谷歌rss。我尝试了jsonp但是它下载了文件并抛出了一个错误"Uncaught SyntaxError: Unexpected token < "

$.ajax({
        type: "GET",
        url:'https://news.google.com/?output=rss',
        //url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),     
        dataType: "xml",
        contentType: "text/xml; charset=utf-8",
        headers: { "Access-Control-Allow-Origin":"*",},                

        success: function(xml) {
        alert("success");
        }   
});

plz guys help me..

请人帮我. .

2 个解决方案

#1


1  

$.getJSON("//ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?", {
    num: 10,
    q: url
}).done(function (data) {
    console.log(data);
});

Notes:

注:

  • You're overdoing it. Don't try to specify information on the client side that the server actually has to supply (content type, allow origin headers, data type).
  • 你过分。不要尝试在客户端指定服务器必须提供的信息(内容类型、允许源头、数据类型)。
  • You don't want XML, you want JSON.
  • 不需要XML,需要JSON。
  • The name for cross-origin JSON requests is JSONP.
  • 跨源JSON请求的名称是JSONP。
  • jQuery implements that for you if you use the getJSON() API method. You don't have to do anything besides adding "callback=?" to the URL.
  • 如果您使用getJSON() API方法,jQuery将为您实现这一点。除了向URL添加“callback=?”之外,您不需要做任何事情。
  • Use jQuery Deferred callbacks (then, done, fail and always). They allow your code to become a lot more flexible.
  • 使用jQuery延迟回调(然后,完成,失败,一直)。它们允许您的代码变得更加灵活。
  • Have a look at the documentation, too. https://developers.google.com/feed/v1/jsondevguide
  • 也看一下文档。https://developers.google.com/feed/v1/jsondevguide

#2


0  

You basically can't implement a web client RSS reader because you can't be sure that content providers will set the correct CORS header for their feed(s) ; My advice would be to not waste your time reading through endless CORS/JSONP lectures (and trying misleading code) but implement a server solution (like, say Pétrolette) and move on.

你基本上不能实现web客户端RSS阅读器,因为你不能确定内容提供者会为他们的提要设置正确的CORS头;我的建议是,不要把时间浪费在阅读没完没了的CORS/JSONP课程(并尝试误导代码)上,而是实现一个服务器解决方案(比如Petrolette),然后继续前进。

#1


1  

$.getJSON("//ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?", {
    num: 10,
    q: url
}).done(function (data) {
    console.log(data);
});

Notes:

注:

  • You're overdoing it. Don't try to specify information on the client side that the server actually has to supply (content type, allow origin headers, data type).
  • 你过分。不要尝试在客户端指定服务器必须提供的信息(内容类型、允许源头、数据类型)。
  • You don't want XML, you want JSON.
  • 不需要XML,需要JSON。
  • The name for cross-origin JSON requests is JSONP.
  • 跨源JSON请求的名称是JSONP。
  • jQuery implements that for you if you use the getJSON() API method. You don't have to do anything besides adding "callback=?" to the URL.
  • 如果您使用getJSON() API方法,jQuery将为您实现这一点。除了向URL添加“callback=?”之外,您不需要做任何事情。
  • Use jQuery Deferred callbacks (then, done, fail and always). They allow your code to become a lot more flexible.
  • 使用jQuery延迟回调(然后,完成,失败,一直)。它们允许您的代码变得更加灵活。
  • Have a look at the documentation, too. https://developers.google.com/feed/v1/jsondevguide
  • 也看一下文档。https://developers.google.com/feed/v1/jsondevguide

#2


0  

You basically can't implement a web client RSS reader because you can't be sure that content providers will set the correct CORS header for their feed(s) ; My advice would be to not waste your time reading through endless CORS/JSONP lectures (and trying misleading code) but implement a server solution (like, say Pétrolette) and move on.

你基本上不能实现web客户端RSS阅读器,因为你不能确定内容提供者会为他们的提要设置正确的CORS头;我的建议是,不要把时间浪费在阅读没完没了的CORS/JSONP课程(并尝试误导代码)上,而是实现一个服务器解决方案(比如Petrolette),然后继续前进。