[英]JSON data to HTML using AJAX
I am trying to input the value of the currency using the Value="AUD" as a starter. 我正在尝试使用Value =“ AUD”作为初始输入货币的价值。 I am very new to JSON and AJAX.
我是JSON和AJAX的新手。 I cannot work out why there is an 404 error linked to JSON.parse and XMLHttpRequest, any advise of where I am going wrong would be much appreciated.
我无法弄清楚为什么存在与JSON.parse和XMLHttpRequest关联的404错误,对我要去哪里的任何建议将不胜感激。 Thanks in advance.
提前致谢。
`enter code here`
<html lang="en">
<head>
</head>
<body>
<div id ="forex-info">
<p id="currencyList" class="currencyList" value ="AUD">Australia</p>
<p id="rateList" class="event"></p>
</div
<script type="text/javascript">
var tableContainer = document.getElementById("forex-info");
var ourRequest = new XMLHttpRequest();
var myData = "http://api.fixer.io/latest".rates;
ourRequest.open('GET', myData, true);
ourRequest.onload = function loading() {
var ourData = JSON.parse(ourRequest.responseText);
renderHTML(ourData);
function renderHTML(data) {
var output = "";
for (var key in data)
{
output += "<p>" + key + output + "</p>"
}
}
};
</script>
</body>
The main issue is how your calling the api " http://api.fixer.io/latest ".rates You call rest endpoints by there address params or with query params. 主要问题是如何调用api“ http://api.fixer.io/latest ” .rates您通过地址参数或查询参数来调用其余端点。
Please see example below calling your specified endpoint. 请参见下面的示例,该示例调用您的指定端点。 That should get you started
那应该让你开始
var myData = 'https://api.fixer.io/latest'
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let res = JSON.parse(xhttp.responseText)
Object.keys(res.rates).forEach((e)=>{
console.log(`${e}: ${res.rates[e]}`)
//Add your stuff here
})
}
};
xhttp.open("GET", myData, true);
xhttp.send();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.