9

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

2
  • 1
    You're missing a } immediately after alert('success') for a start. Commented Mar 25, 2014 at 12:57
  • I am having the same issue any idea ? I am using yahoo news feeds and working with JSONP. Commented Apr 16, 2017 at 6:57

2 Answers 2

3
$.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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanx for reply. but i want to get rss feed without using Google Feed API.
Well, there's two ways this can go: A) If the remote server implements JSONP then you can use the above method. B) If the remote server does not implement JSONP then you are out of luck. There is no way around the same origin policy.
RSS, as others have noted, is not cross-origin friendly. Goole's Feed API is going away. RSS to API is a simple app you can host on Heroku or wherever that provides an API that your JavaScript can query to access RSS feeds. This gets around those pesky cross-origin restrictions. I authored it for a similar use case.
@aspiringwebdev That's a misleading statement. RSS as a technology is not at all cross-origin unfriendly. Simply setting correct CORS headers will make an RSS feed cross-origin friendly, just like with any other HTTP resource. It's the content providers of RSS feeds that are cross-origin unfriendly, out of ignorance or laziness.
@Tomalak you're absolutely right that it's the content providers and not RSS itself that often block cross-origin requests. In my experience, though, it's unusual for a content provider to have cross-origin-friendly CORS headers for its RSS feed.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.