简体   繁体   中英

Read single data in csv data downloaded from url

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

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM