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)
这可能是因为您的前端服务地址端口(3000)和后端服务地址端口(8000)不同,从而触发CORS错误。 您需要了解如何从 Python API 启用 CORS。
如何在python中启用CORS 检查这是否有帮助