An Api from Currencylayer

问题内容:

Currency layer provided me with a LIVE API where I can get current exchange rates. So I decided to to use this live API for currency conversion (i’m beginner on programming trying to learn and improve)


I changed the code to some extent. I have limited knowledge of jQuery . If somebody is willing to help, the task is, if user writes some number for example 7000 on AFN INPUT,then it should show 85.99 EUR and 101.50 USD, i know my code isnt 100 right. Below are the codes.
API received from them:

// set endpoint and your access key
endpoint = 'live'
access_key = 'YOUR_ACCESS_KEY';

// get the most recent exchange rates via the "live" endpoint:
$.ajax({
    url: 'http://apilayer.net/api/' + endpoint + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {

        // exchange rata data is stored in json.quotes
        alert(json.quotes.USDGBP);
        
        // source currency is stored in json.source
        alert(json.source);
        
        // timestamp can be accessed in json.timestamp
        alert(json.timestamp);
        
    }
});

// set endpoint and your access key
endpoint = 'convert';
access_key = 'YOUR_ACCESS_KEY';

// define from currency, to currency, and amount
from = 'EUR';
to = 'GBP';
amount = '10';

// execute the conversion using the "convert" endpoint:
$.ajax({
    url: 'http://apilayer.net/api/' + endpoint + '?access_key=' + access_key +'&from=' + from + '&to=' + to + '&amount=' + amount,   
    dataType: 'jsonp',
    success: function(json) {

        // access the conversion result in json.result
        alert(json.result);
                
    }
});

What I did:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <style> 
  </style>
</head>
<body>
<script>
function convert(degree) {
    var x;
if (degree == "afn") {
    x = document.getElementById("afn").value;

    document.getElementById("eur").value = (x).toFixed(2);
    x = document.getElementById("afn").value;
    document.getElementById("usd").value = (x).toFixed(2);
}else if(degree == "eur") {
    x = document.getElementById("eur").value;
    document.getElementById("afn").value = (x).toFixed(2);
    x = document.getElementById("eur").value;
    document.getElementById("usd").value = (x).toFixed(2);
}else if(degree == "usd") {
    x = document.getElementById("usd").value;
    document.getElementById("eur").value = (x).toFixed(2);
    x = document.getElementById("usd").value;
    document.getElementById("afn").value = (x).toFixed(2);
}}
// set endpoint and your access key
endpoint = 'live'
access_key = 'YOUR_ACCESS_KEY';  // can't reveal API key

var from = $('#afn').val();
var to = $('#eur').val();
var combine = from+to;

$.ajax({
    url: 'http://apilayer.net/api/' + endpoint + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {
    // exchange rata data is stored in json.quotes
    //var response = json.quotes.combine;
    alert(json.quotes.combine); 
    $('#convert').html('response');
}
}); 
</script>


  <form >
    <div class="form-group">
     <input class="form-control" id="afn" onkeyup="convert('afn')" placeholder="Enter Afghani" ><p>AN</p>
    </div>
	 <input type="email" class="form-control" id="eur"  onkeyup="convert('eur')" placeholder="Enter Euro" ><p>€</p> 
    </div>
	
    <input type="email" class="form-control" id="usd"  onkeyup="convert('usd')" placeholder="Enter US Dollar"><p>$</p> 
   </div>
   
  </form>
</div>
</body>
</html>

问题评论:

    
In your real code, do have you inserted the correct value for ‘YOUR_ACCESS_KEY’ in both scripts?

原文地址:

https://stackoverflow.com/questions/47756645/an-api-from-currencylayer

添加评论