javascript - 无法调用 API 和接收汇率
问题描述
我目前正在学习 Java、HTML 和 CSS,因此我开始制作一个货币转换器来将这些技能联系在一起,尽管我在调用 API 和打印相关数据(汇率)时遇到了麻烦。下面是我的代码:
<html>
<head>
<title>API Test</title>
</head>
<body>
<script>
endpoint = 'latest'
access_key = 'xxx';
// get the most recent exchange rates via the "latest" endpoint:
$.ajax({
url: 'http://data.fixer.io/api/' + endpoint + '?access_key=' + access_key,
dataType: 'jsonp',
success: function(json) {
// exchange rata data is stored in json.rates
alert(json.rates.GBP);
// base currency is stored in json.base
alert(json.base);
// timestamp can be accessed in json.timestamp
alert(json.timestamp);
</script>
</body>
</html>
以下代码根本不打印任何内容,我不确定这是为什么。上面的代码直接来自 api 文档,尽管作为初学者,我觉得我可能遗漏了一些明显的东西。任何有关为什么会发生这种情况的帮助将不胜感激。
解决方案
@Andy58,jquery 函数关闭中断似乎有问题,可能是你忘记包含 jquery 所以尝试使用给定的 html
<html>
<head>
<title>API Test</title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
</script>
</head>
<body>
<script>
endpoint = 'latest'
access_key = 'xxx';
// get the most recent exchange rates via the "latest" endpoint:
$.ajax({
url: 'http://data.fixer.io/api/' + endpoint + '?access_key=' + access_key,
dataType: 'json',
success: function(json) {
alert(json);
// exchange rata data is stored in json.rates
alert(json.rates.GBP);
// base currency is stored in json.base
alert(json.base);
// timestamp can be accessed in json.timestamp
alert(json.timestamp);
}
});
</script>
</body>
</html>
推荐阅读
- javascript - 我应该使用 JQuery 对话框来显示/提交表单元素吗?
- android - Dokka 失败:com.intellij.psi.impl.source.PsiClassImpl 无法转换为 com.intellij.psi.PsiJavaFile
- java - 为什么 Java8 Stream.min() 和 max() 将比较器作为输入?
- android - 方向按钮在“负”方向上不起作用
- android - 将布局动态添加到具有一些 ID 问题的 Linearlayout
- php - 替换数组 PHP 中的一个单词
- java - 为什么我的 Android Studio 应用无法在 Android 设备上运行?
- selenium-webdriver - 如何解决此问题,即尝试单击单击时展开的按钮时出现“无法定位元素”错误
- sql - 在 SQL Server 2014 中比较表列并显示错误消息中的差异
- java - 对等方的证书具有无效签名。错误代码:Selenium GeckoDriver 和 Firefox 通过 Java 出现 SEC_ERROR_BAD_SIGNATURE 错误