2

I'm trying to fetch the data from Yahoo! Finance to get value for currency exchange with this url assuming I'm getting currency exchange for USD to EUR in javascript

http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1

in the csv, I would receive just the one value I need. Eg. - '0.8994'

Now, in same javascript file, I want this value to be pass into a variable

var USDEUR;

at this point, I'm not sure how to pass the value from the downloaded csv. Could someone enlighten me how to do this properly?

Thanks in advance

flagstar
  • 27
  • 6
  • possible duplicate of [Receive .csv file as data in ajax success function](http://stackoverflow.com/questions/12250065/receive-csv-file-as-data-in-ajax-success-function) – S McCrohan Apr 30 '15 at 02:53

2 Answers2

0

It seems you don't actually need to parse this file as a csv, it's basically a plain text string? Assuming jquery:

var USDEUR;
$.ajax({
    type: "GET",
    url: "http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1",
    dataType: "text",
    success: function(data) {USDEUR = data}
 });

EDIT: It seems the url has access control issues, you may not be able to do this with a XHTTP request from the client. Is this data meant to be accessed as a public API?

Dylan Watt
  • 3,357
  • 12
  • 16
  • Those URL are public as far as I know since I'm referring to this link https://code.google.com/p/yahoo-finance-managed/wiki/YahooFinanceAPIs – flagstar Apr 30 '15 at 02:55
  • It appears to be that it's public, but that they haven't enabled CORS, so you can't access from a cross domain in a browser. You'll have to fetch it server side and proxy it to your client: http://stackoverflow.com/questions/13755038/cross-domain-get-csv-file – Dylan Watt Apr 30 '15 at 02:57
0

Use JQuery Ajax

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
var USDEUR;
$.get("download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1", function(data){
  USDEUR = $.parseXML(data);
}, "xml");
</script>
Lukas Greblikas
  • 649
  • 6
  • 14