1

I am trying to read an RSS feed (here, the Google News feed) and get specific info out of the raw code using Javascript. The below code works when the URL in the second line is changed to a local file (I copy-pasted the gNews and saved as xml) in Firefox, but not when it is an external page as below. The code does not work under any conditions in IE or Chrome. Any suggestions?

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","https://news.google.com/news/feeds?ned=us&topic=h&output=rss",false);
try {
    xmlhttp.send();
}
catch(exception){
    document.write(exception+"");
}
var xmlDoc=xmlhttp.responseXML; 
var x=xmlDoc.getElementsByTagName("item");
for (i=0;i<4;i++) {
  if(i%2==0) {
    document.write("<div id='bottombox'>");
  }
  var raw=x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;<br />
  var toWrite=""+raw.substring(0,raw.indexOf(" - ",0));
  document.write("<a href="+x[i].getElementsByTagName('link')[0].childNodes[0].nodeValue+" style='font-family:Verdana, Geneva, sans-serif; font-size:13px; color:#FFF;'>");
  document.write(toWrite);
  document.write("</a>");
  document.write("<br />");
  if(i%2!=0) {
      document.write("</div>");
  }
  }
2
  • 1
    Look in your browser's error console. Commented Jun 1, 2012 at 17:20
  • You might consider setting up a server proxy for grabbing the feeds and making them available to your JavaScript. One option is RSS to API, which can be deployed to Heroku in one click. (Disclaimer: I'm the author) Commented Sep 11, 2015 at 21:38

1 Answer 1

1

This is a cross-origin security violation, and prohibited by your browser.

See "Same Origin Policy" and "Cross Origin Resource Sharing" for more information.

Sign up to request clarification or add additional context in comments.

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.