我有一个MERN堆栈应用程序,我面临这个问题:
- 语法错误:JSON.parse: JSON数据的第1行第1列出现意外字符**
这是我在前端的代码
import React, { useEffect, useState } from "react";
const Home = () => {
const [workouts, setWorkouts] = useState(null);
useEffect(() => {
const fetchWorkouts = async () => {
const response = await fetch("/api/workouts");
const json = await response.json();
if (response.ok) {
setWorkouts(json);
}
};
fetchWorkouts();
}, []);
return (
<div>
<div>
{workouts &&
workouts.map((workout) => <p key={workout._id}>{workout.title}</p>)}
</div>
</div>
);
};
export default Home;
这是console.log(response)的输出
响应{type:"basic",url:"http://localhost:5173/api/workouts",重定向:false,状态:200,ok:true,statusText:"OK",标题:集管(8),主体:ReadableStream,bodyUsed:false} Home. jsx:9:14
我的节点服务器返回一个有效的json(数组),类似于[{"_id ":"648b46c2b757e7cd377c3fc5","title":“卧推”,“重复次数”:15、“加载”:0,"createdAt":“2023 - 06 - 15T17:十三:38.124Z "," updatedAt ":“2023 - 06 - 15T17:十三:38.124Z","__v":0},{"_id ":" 648b44d9e5cf2c8682e266b5","标题":"Running "," reps ":1000,“加载”:0," createdAt ":“2023 - 06 - 15T17:05:29.790Z","updatedAt":“2023 - 06 - 15T17:05:29.790Z","__v":0},{"_id":" 648b43288f596df8bbe0355f","标题":"仰卧起坐","重复":70、“负载”:0," createdAt":“2023 - 06 - 15T16:五十八:16.244Z"," updatedAt":“2023 - 06 - 15T16:五十八:16.244Z","__v":0},{"_id":" 648b428a8f596df8bbe0355c"," title":“俯卧撑”,“重复”:100,“加载”:100," createdAt":“2023 - 06 - 15T16:五十五:38.480Z"," updatedAt":“2023 - 06 - 15T17:三十七:02.415Z","__v":0}]
谁能帮帮我?谢谢<3
我什么都没做
1条答案
按热度按时间qgelzfjb1#
我在我的节点服务器上安装了cors包
npm i cors