我已经从货币层获得了免费会员资格。我想兑换货币,但它不允许在免费会员。相反,他们为我提供了一个LIVE API,在那里我可以获得当前的汇率。因此,我决定通过对jQuery代码执行一些tic tac来使用此实时API进行货币转换。我在某种程度上修改了代码,我想我差一点就成功了(也许我错了)。我对jQuery的工作知识有限,所以我在最后一点被卡住了。以下是该问题的代码和解释。
从他们那里收到的API
// set endpoint and your access key
endpoint = 'live'
access_key = 'YOUR_ACCESS_KEY'; // can't reveal API 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 = 'live'
access_key = 'YOUR_ACCESS_KEY'; // can't reveal API key
var from = $('#from').val();
var to = $('#to').val();
var combine = from+to;
// 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
//var response = json.quotes.combine;
alert(json.quotes.combine); // HERE IN PLACE OF USDGBP I USED THE COMBINE VARIABLE
$('#convert').html('response');
}
}); 上面使用的combine变量返回未定义的。也许我没有正确编码,因为我的jquery知识有限。如果我能修复这个问题,那么我很可能就能使用代码了。请帮帮我。
发布于 2017-03-01 16:08:32
要通过保存其名称的变量访问对象属性,您需要使用方括号表示法。要设置input的值,请使用val()。试试这个:
success: function(json) {
var value = json.quotes[combine];
$('#convert').val(value);
}https://stackoverflow.com/questions/42526289
复制相似问题