Savio menezes
Savio menezes

Reputation: 140

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.

Upvotes: 0

Views: 1387

Answers (2)

curv
curv

Reputation: 3844

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>

Upvotes: 3

Rajkumar Somasundaram
Rajkumar Somasundaram

Reputation: 1270

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>

Upvotes: 0

Related Questions