首页 > 解决方案 > 如何在新选项卡上使用 axios 输出

问题描述

我想用户单击按钮,然后 Axios 输出将显示在新选项卡上(在 about:blank 或类似的地方)

公牛:

click() {
    this.setState({ isLoading: true });

    axios
    .get(
      `http://localhost:5000/model-list`
    )
    .then((res) => {
      this.setState({
       inferenceout: res.data , isLoading: false,
      })
     
      let newWin = window.open("about:blank", "res.data", "width=400,height=400");
      newWin.document.write(JSON.stringify(res.data))
    })
}

它目前在新选项卡中返回输出 JSON,但我希望我在下面编写的 HTML 脚本将在新选项卡中,请帮助我

标签: reactjsaxios

解决方案


您要传递给新窗口的 HTML 元素是什么?

您可以在 JavaScript 中使用字符串插值来通过 HTML 元素传递数据,如下所示,

newWin.document.write(`<html><p>Whatever HTML you want and the ${res.data.name} you wanted</p></html>`)

如果您想要整个对象,您可以执行以下操作,

newWin.document.write(`<html><p>Whatever HTML you want and the ${JSON.stringify(res)}</p></html>`)

在此处输入图像描述


推荐阅读