0

I'm trying to get a bit of javascript working to pull back a currency conversion.

Using the API from fixer.io, I'm requesting http://api.fixer.io/latest?symbols=USD&base=GBP

I get an object back, but I don't know how to drill down to the currency value I need.

My code so far:

function GetMarketRate() {

    var url = 'http://api.fixer.io/latest?symbols=_CURR&base=GBP'.replace("_CURR", "@Model.CurrencyId");

    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);

    xhr.onload = function () {
        var res = window.JSON.parse(xhr.responseText);

        //Drill down

    };

    xhr.onerror = function () {
        //Process error
    };

    xhr.send();

};

I'm using MVC and Razor to amend the URL to get the Currency. I will then take the decimal value and insert it into an inline div as the page loads.

The only problem I have is actually getting to that value. My API request is such that it will only ever pull back one value - don't know if that helps things.

Stuart Frankish
  • 818
  • 1
  • 11
  • 27

1 Answers1

0

The returned object looks like:

{"base":"GBP","date":"2015-06-18","rates":{"USD":1.5912}}

So you should be able to use:

res.rates.USD // 1.5912
Jason Cust
  • 10,743
  • 2
  • 33
  • 45