weixin_33709590 2017-04-09 16:59 采纳率: 0%
浏览 159

使用AJAX将JSON数据转换为HTML

I am trying to input the value of the currency using the Value="AUD" as a starter. I am very new to JSON and 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. 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>

  • 写回答

1条回答 默认 最新

  • weixin_33696822 2017-04-09 17:24
    关注

    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.

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: