Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 311
NHD Avatar asked Mar 25 '14 12:03

NHD


2 Answers

$.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
like image 101
Tomalak Avatar answered Nov 12 '22 04:11

Tomalak


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.

like image 38
yPhil Avatar answered Nov 12 '22 05:11

yPhil