5

I am working on html & js in which i display yahoo finance stock in table format. The data get in csv. I want js directly read data from url

The url is http://ichart.finance.yahoo.com/table.csv?s=RIL.BO

The code i try which i get from stackoverflow is working in localhost url.

 var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "http://ichart.finance.yahoo.com/table.csv?s=RIL.BO", true);
    txtFile.onreadystatechange = function() {
      if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
        if (txtFile.status === 200) {  // Makes sure it's found the file.
          allText = txtFile.responseText;
          lines = txtFile.responseText.split("\n"); // Will separate each line into an array
          alert(allText);
        }
      }
    }

Thanks

2
  • 2
    some browser not allow cross domain ajax call. use jsonp instead of it
    – rab
    Aug 1, 2013 at 5:22
  • Gurjinder: Can you upvote or accept any of the answers that were helpful? Sep 3, 2013 at 8:22

2 Answers 2

6

In order to get around the Cross Domain request restrictions put in place by the Same Origin Policy, you need an endpoint that allows you to do a JSONP request or that has enabled CORS. Unfortunately, the Yahoo! Finance endpoint has neither.

So, as James mentioned, you ned a middle man.

Usually, my recommendation for this is to use YQL, which allows you to quickly and easily build a server that sits between you and the finance site. In fact, they already have a Yahoo! Finance endpoint for exactly the data you're trying to get: link

However, as that can be unreliable, I also have a website scraper that I've used in various projects. It's hosted on Heroku and allows you to fetch almost any content from any site. I don't recommend using it for high volume projects, but for occaisional data fetches it's great. In your case, you would use it like this:

http://websitescraper.herokuapp.com/?url=http://ichart.finance.yahoo.com/table.csv?s=RIL.BO&callback=jsCallback

Edit: ichart.finance.yahoo.com has been deprecated, so this fails. Keeping it here for reference

Now that you have that out of the way, I recommend using jQuery and the csv-to-array plugin:

jQuery.getJSON('http://websitescraper.herokuapp.com/?url=http://ichart.finance.yahoo.com/table.csv?s=RIL.BO&callback=?', function (csvdata) {
  console.log(csvdata.csvToArray());
});

Also, if you want to launch your own middle man, you can use the website-scraper that I've built. The source code is on GitHub and it's released under the MIT license.

3
  • 2
    Checked out the code; nice simple scraper that would be good for Gurjinder to get started with. Aug 7, 2013 at 4:58
  • Your website scraper's certificate is expired :( Oct 25, 2018 at 8:53
  • @ErwinRooijakkers Thanks Erwin. Looks like heroku has dropped support for apps being hosted at heroku.com. I've updated the link to be websitescraper.herokuapp.com Nov 1, 2018 at 16:04
2

You are trying to do a cross domain request so its being blocked.

You will need to write a server side script to fetch the data for you.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.