最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to retrieve exchange rates with Javascript? - Stack Overflow

programmeradmin2浏览0评论

I am trying to get a simple exchange rate from USD to MXN using yahoo api but Im not getting anything back, is this just a CORS issue again or am I accessing the property incorrectly?

var exchangeRateURL = "*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDMXN%22%29&env=store://datatables/alltableswithkeys";

//Get current USD->MXN exchange rate
$.getJSON(exchangeRateURL, function(data) 
    {   
        priceInMXN = data.query.results.rate.Rate;
        document.write(priceInMXN);

    });     

Thanks in advance!

I am trying to get a simple exchange rate from USD to MXN using yahoo api but Im not getting anything back, is this just a CORS issue again or am I accessing the property incorrectly?

var exchangeRateURL = "http://query.yahooapis./v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDMXN%22%29&env=store://datatables/alltableswithkeys";

//Get current USD->MXN exchange rate
$.getJSON(exchangeRateURL, function(data) 
    {   
        priceInMXN = data.query.results.rate.Rate;
        document.write(priceInMXN);

    });     

Thanks in advance!

Share Improve this question asked Dec 3, 2015 at 4:48 Noah-1Noah-1 3961 gold badge5 silver badges21 bronze badges 1
  • 1 What does your console say? Also that endpoint appear to provide XML, not JSON. – Marty Commented Dec 3, 2015 at 4:51
Add a ment  | 

2 Answers 2

Reset to default 3

Pass a param to return JSON (&format=json):

var exchangeRateURL = "http://query.yahooapis./v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDMXN%22%29&env=store://datatables/alltableswithkeys&format=json"

$.getJSON(exchangeRateURL, function(data) {
  console.log(data.query.results.rate.Rate);
});    

You're fetching XML, not JSON. Something like this will work:

$.get(exchangeRateURL, function(data) {
    console.log($(data).find('Rate').text());
});
发布评论

评论列表(0)

  1. 暂无评论