我试图使用axios将我的react前端与java后端连接起来。但我陷入了“'then'未定义”运行时错误的发生

问题描述 投票:0回答:1
function HelloWorldRestApiCall() {
    axios.get('http://localhost:8080/hello-world')
         .then((response) => successfulResponse(response))
         .catch((error) => errorResponse(error))
         .finally(() => console.log("clean-up"))
}

function successfulResponse(response) {
    console.log(response)
    // setMessage(response.data)
    setMessage(response.data)
}

我尝试使用不同的网址,但不起作用。

reactjs http axios
1个回答
0
投票

目前尚不清楚您的情况下 axios 是什么,但我建议尝试以下操作:

  • 启动处的某个地方:
    import axios from 'axios';

    const api = axios.create();
  • 在您的组件中:
    import { api } from 'src/boot/axios';

    function HelloWorldRestApiCall() {
        api.get('http://localhost:8080/hello-world')
             .then((response) => successfulResponse(response))
             .catch((error) => errorResponse(error))
             .finally(() => console.log("clean-up"))
    }

此外,尝试使用路由,以便您从

/hello-world
发出请求,并将
/
重新路由到
http://localhost:8080
作为开发服务器,这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.