首页 > 解决方案 > 我无法获取 JSON 数据

问题描述

我正在尝试包含 JSON 数据,但我的跨度为空。你知道我哪里错了吗?刚才提到我有访问密钥,当我调用 JSON 时,我将它包含在 url 中。

JSON

{
"status": true,
"msg": "successfully",
"response": [
{
"id": "1",
"price": "1.1111",
"change": "+0.0008",
"chg_per": "+0.07%",
"last_changed": "2020-01-09 14:12:02",
"symbol": "EUR/USD"
}
],
"info": {
"server_time": "2020-01-09 14:12:06 UTC"
}
}

JS

<script>
        const api_url = "https://fcsapi.com/api/forex/latest?id=1"
        async function getFX() {
            const response = await fetch(api_url);
            const data = await response.json();
            const { price } = data;

            document.getElementById('bid').textContent = price;
        }
        getFX();

        setInterval(getFX, 3000);
    </script>

HTML

<span class="py-1 px-2 w-full block rounded bg-dark-800 flex justify-end items-center" id="bid">
                                            <img class=" ml-1 mt-0" src="images/svg/arrowDown.svg" style="width: 9px;height: 10px;">

                                        </span>

标签: jsongetforex

解决方案


你应该这样做document.getElementById('bid').textContent = JSON.stringify(price)。因为您首先必须将该对象转换为字符串。

或者你也可以先试试看price有没有东西再试试 document.getElementById('bid').innerText = JSON.stringify(price)


推荐阅读