MongoDB 与 React 集成 [已关闭]

问题描述 投票:0回答:1

React 应用程序使用 python 与后端连接。从数据库中获取下拉数据。下面是ui代码-

useEffect(() => {
    const fetchLocations =  () => {
      try {
        const response =  fetch('http://localhost:8000/locations');
        const data = response.json();
        console.log(data);
        setLocations(data);
      } catch (error) {
        console.error('Error fetching locations:', error);
      }
    };

    const fetchQualifications = async () => {
      try {
        const response = await fetch('http://localhost:8000/qualifications');
        const data = await response.json();
        setQualifications(data);
      } catch (error) {
        console.error('Error fetching qualifications:', error);
      }
    };
  })

服务器正在运行。在 url 中我可以看到 json 格式的数据。问题与前端有关,我没有获取数据。 控制台出错 -

GET http://localhost:5000/qualifications net::ERR_FAILED 200 (OK)

python reactjs node.js mongodb
1个回答
0
投票

这可能是因为您的前端服务地址端口(3000)和后端服务地址端口(8000)不同,从而触发CORS错误。 您需要了解如何从 Python API 启用 CORS。

如何在python中启用CORS 检查这是否有帮助

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