javascriptjqueryajaxcurrency-exchange-rates

How to extract converted value from response using jquery ajax method?


I am using this link :freecurrencyconverterapi to get the converted value from USD to INR.

As you can see in developer mode of browser that the response is {"USD_INR":64.857002}. Since I am new to programming, is there a way to get the float value using jquery ajax .

Thanks in advance.


Solution

  • That is returning a JSON object.

    You need to assign that response object to a variable in your code, so ultimately it will end up looking like below...

    var currency = { USD_INR: 64.857002 };
    

    Then you can access it like this:

    currency.USD_INR // This will give you 64.857002
    

    See example below..

    Edit: As per Rory's code (adapted)...

    var currency;
    
    $.ajax({
      url: 'https://free.currencyconverterapi.com/api/v4/convert?q=USD_INR&compact=ultra',
      dataType: 'jsonp',
      success: function(data) {
        currency = data.USD_INR;
        console.log(currency);
      }
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>