【问题标题】:Display js Data in div Html在 div Html 中显示 js 数据
【发布时间】:2021-10-25 22:18:59
【问题描述】:

我想在控制台日志中显示我从这段代码中获得的数据(它是 JSON 格式)并在 HTML 中显示它。我应该使用哪种方法?

async function getAVA() {

    fetch('https://graphql.avascan.info', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
        query: `
        query {
            stats {
                priceAvaxUsd
            }
        }`
        }),
        })
        .then((res) => res.json())
        .then((result) => console.log(result));
  }

  getAVA();

【问题讨论】:

    标签: javascript jquery json fetch-api


    【解决方案1】:

    async function getAVA() {
    
    fetch('https://graphql.avascan.info', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            query: `query {stats {priceAvaxUsd}}`
        }),
    })
        .then((res) => res.json())
        .then((result) => {
            console.log(result);
            document.getElementById('result').innerHTML = result.data.stats.priceAvaxUsd;
        })
        .catch(error => {
            console.log(error);
        })
    }
    
    getAVA();
    <div id="result"></div>

    【讨论】:

    • 我修正了答案
    • 语法错误,意外令牌 {}。这是日志显示的内容
    • 它有效,但它显示来自控制台的所有数据,我只想显示 priceAvaxusd 数字。
    猜你喜欢
    • 2019-05-20
    • 2017-11-06
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    相关资源
    最近更新 更多