首页 > 解决方案 > 如何使用 Rapidapi 从 json 获取 html 输出?

问题描述

我一直在努力从Json文件和我从 rapidapi 使用的 API 中获取 HTML 表中的输出,但我没有得到任何输出,也没有代码错误。有什么错误吗?任何人请帮助!

$.getJSON(settings).done(function(response){ 
console.log(response) });

这将日志提供给控制台..但其他事情没有..为什么?我一直试图获得输出 2 天,但失败了。

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> Title </title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">



<script type="text/javascript">
    $(function(){
        var settings = {
            "async": true,
            "crossDomain": true,
            "url": "rapidapi_url",
            "method": "GET",
            "headers" :{
                "x-rapidapi-host": "API_url",
            "x-rapidapi-key": "my_api_key"
            }
        };
        $.getJSON(settings, function(data){
            $.each(data.country_stats, function(i, f){
            console.log(i);
            console.log(f);

                var tblrw = "<tr>" + "<td>" + value.country_name + "</td>" + "<td>" + value.cases + "</td>" + "<td>" + value.deaths + "</td>" + "<td>" + value.region + "</td>" + "<td>" + value.total_recovered + "</td>" + "<td>" + value.new_deaths + "</td>" + "<td>" + value.new_cases + "</td>" + "<td>" + value.serious_critical + "</td>" + "<td>" + value.total_cases_per_1m_population + "</td>" + "</tr>"
                $(tblrw).appendTo("#tablerw tbody");

            }); 
        });

        var tblrw = "<tr>" + "<td>" + value.country_name + "</td>" + "<td>" + value.cases + "</td>" + "<td>" + value.deaths + "</td>" + "<td>" + value.region + "</td>" + "<td>" + value.total_recovered + "</td>" + "<td>" + value.new_deaths + "</td>" + "<td>" + value.new_cases + "</td>" + "<td>" + value.serious_critical + "</td>" + "<td>" + value.total_cases_per_1m_population + "</td>" + "</tr>";
        $(tblrw).appendTo("#tablerw tbody");   
    });
</script>
</head>
<body>
<table class="table table-bordered" id="tablerw">
<thead>
<th>Country name</th>
<th>cases</th>
<th>Deaths</th>
<th>Region</th>
<th>Total Recovered</th>
<th>New Deaths</th>
<th>New cases</th>
<th>Serious/Critical</th>
<th>Total cases per 1m</th>
    </thead>
<tbody >

</tbody>
</table>


</body>
</html>

标签: javascriptjqueryhtmljsonajax

解决方案


推荐阅读