0

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.

Community
  • 1
  • 1
Savio menezes
  • 140
  • 1
  • 11
  • Here's a different API that works as-is: https://jsfiddle.net/khrismuc/71qt68xt/ –  Nov 22 '17 at 10:30

2 Answers2

3

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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
curv
  • 3,796
  • 4
  • 33
  • 48
0

The url that u provided had an issue, so I took an unorthodox route to get what u wanted;

$.get( "https://cors-anywhere.herokuapp.com/https://free.currencyconverterapi.com/api/v4/convert?q=USD_INR&compact=ultra", function( data ) {
  $( ".result" ).text( data );
  document.getElementById("result").innerHTML = JSON.stringify(data);
  console.log(data);
  //alert( "Load was performed." );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="result">
</div>
Rajkumar Somasundaram
  • 1,225
  • 2
  • 10
  • 20