【问题标题】:why is react axios call returning empty result?为什么反应 axios 调用返回空结果?
【发布时间】:2020-04-29 01:22:48
【问题描述】:

我有一个 Spring Boot 应用程序在 localhost:8080 的后端运行。前端是 localhost:3000 上的 react 应用程序。我正在尝试从后端调用 url,但结果为空。为什么?

如果我像这样在浏览器中直接调用后端 url:

http://localhost:8080/appointments

我看到数据正常(以视图模板的形式),如下所示:

但是当我像这样从我的 react 应用调用相同的 url 时:

    useEffect(() => {
    (
        async () => {
            const result = await axios("http://localhost:8080/appointments");
            console.log("result="+result);
            //setData(result.data);
        }
    )();
}, 

然后控制台显示并清空结果:

result=[object 对象]

抱歉,这是一个基本问题。我是新手。

【问题讨论】:

标签: reactjs axios


【解决方案1】:

你忘了在 axios 中提到 get 请求。

试试这个方法:

    useEffect(() => {
    (
        async () => {
            const result = await axios.get("http://localhost:8080/appointments");
            console.log("result =", result);
            //setData(result.data);
        }
    )();
}, 

另外,当您 console.log("result="+result); 将对象连接到字符串时,请改用 console.log("result =", result);

【讨论】:

  • 我想我知道一次然后忘记了。谢谢!不幸的是,它没有返回我所期望的。它返回整个 html 表,而不仅仅是数据。我需要深入挖掘。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
相关资源
最近更新 更多