简体   繁体   中英

Read RSS XML in javascript (cross domain)

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

$.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..

$.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.
  • The name for cross-origin JSON requests is 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.
  • Use jQuery Deferred callbacks ( then , done , fail and always ). They allow your code to become a lot more flexible.
  • Have a look at the documentation, too. https://developers.google.com/feed/v1/jsondevguide

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM