【问题标题】:Javascript Fetch Function returns [object Promise]Javascript Fetch 函数返回 [object Promise]
【发布时间】:2022-11-14 06:39:22
【问题描述】:

我目前正在开发一个项目,其中包括一个由 Django 构建和运行的网站。在这个网站上,我正在尝试通过快速 API 加载数据,并尝试通过 JavaScript 和 Fetch API 加载这些数据。但我总是得到一个 [object Promise] 而不是通过 API 提供的数据。我尝试了许多不同的方法,但似乎都没有奏效。

任何帮助将不胜感激!

我试过例如:

document.getElementById("1.1").innerHTML = fetch('the URL')
 .then(response => response.text())

或者

document.getElementById("1.1").innerHTML = fetch('the URL')
.then(response => response.text())
.then((response) => {
    console.log(response)
})

和许多其他方法。我也检查过,API 请求运行良好,返回一个字符串。

【问题讨论】:

    标签: javascript html django fetch-api


    【解决方案1】:

    您希望在记录最终响应时显示 html 的设置,例如:

    fetch('the URL')
    .then(response => response.text())
    .then((response) => {
        console.log(response)
        document.getElementById("1.1").innerHTML = response
    })
    
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 1970-01-01
      • 2020-04-03
      • 2021-08-16
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      相关资源
      最近更新 更多