代理错误:无法代理请求/从Localhost开始:3000到http:// localhost:5000

发布于 2025-01-20 13:18:09 字数 1081 浏览 4 评论 0原文

我正在研究一个简单的 react-flask应用,旨在从后端获取当前时间并将其显示在前端。

我有烧瓶后端,而前端的反应都同时奔跑。

后端在端口5000上的工作正常很好:

back-end

从前端发出的调用'/time'也无法获取当前时间,即使我在package.json.json

  "proxy": "http://localhost:5000"

前端 定义了我的代理:

function App() {
  const [currentTime, setCurrentTime] = useState(0);

  const getCurrentTime = async (API) => {
    const response = await fetch(API);
    const jsonData = await response.json();
    setCurrentTime(jsonData.time);
    console.log(jsonData);
  };

  useEffect(() => {
    // getCurrentTime('http://localhost:5000/time');
    getCurrentTime('/time');
  }, []);

我尝试了讨论的方法。但是他们似乎都不对我有用。

I am working on a simple React-Flask App which aims to fetch the current time from the Back-end and display it on the Front-end.

I have the Flask Back-end and the React Front-end both running together at the same time.

The back-end is working perfectly fine on port 5000:

Back-end

Fetch call '/time' from the front-end is unable to fetch the current time even tho I have my proxy defined in the package.json:

  "proxy": "http://localhost:5000"

Front-end:

function App() {
  const [currentTime, setCurrentTime] = useState(0);

  const getCurrentTime = async (API) => {
    const response = await fetch(API);
    const jsonData = await response.json();
    setCurrentTime(jsonData.time);
    console.log(jsonData);
  };

  useEffect(() => {
    // getCurrentTime('http://localhost:5000/time');
    getCurrentTime('/time');
  }, []);

I have tried the methods discussed here. But none of them seems to work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2025-01-27 13:18:09

“代理”:“http://127.0.0.1:5000”。这个解决方案对我有用。我收到此错误的原因是我不知道在 package.json 中进行更改后必须重新启动开发服务器。

这对我有用

"proxy": "http://127.0.0.1:5000". this solution worked for me. The reason why I was getting this error is that I didn't know that I have to restart the development server after making changes in the package.json.

This worked for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文