Currency Conversion Using jQuery API

Asked

Viewed 293 times

2

I am making a conversion from Chinese Currency (Yuan) to Brazilian Currency (Real). I am using the website API: https://free.currencyconverterapi.com. Turns out I can’t set the value to be converted, the conversion only returns the result of a value conversion 1. As if converting 1 to currency Real. But in my code I want to convert 200 to currency Real. Code:

$(document).ready(function(){

	$('#resultado').html('Convertendo...');
	
	var moeda = { CNY_BRL: 200 };
	
	$.ajax({
	  url: 'https://free.currencyconverterapi.com/api/v6/convert?q=CNY_BRL&compact=ultra&apiKey=873041c527593ec7e31e',
	  dataType: 'jsonp',
	  success: function(data) {
		moeda = data.CNY_BRL;
		console.log(moeda);
		 $( "#resultado" ).html(moeda);
	  }
	})
	
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
O valor convertido é: <span id="resultado"></span>

1 answer

1


I made an example below, analyzing the api I realized that it does exactly what you reported, but you must multiply the value to be converted by the result, as I did below!

$(document).ready(function(){

	$('#resultado').html('Convertendo...');
	   	
	$.ajax({
	  url: 'https://free.currencyconverterapi.com/api/v6/convert?q=CNY_BRL&compact=ultra&apiKey=873041c527593ec7e31e',
	  dataType: 'jsonp',
	  success: function(data) {
		moeda = data.CNY_BRL;
		console.log(moeda*200);
		 $( "#resultado" ).html(moeda*200);
	  }
	})
	
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.