FastAPI 后端和 Next.js 前端都在localost. 在同一台计算机上,前端可以fetch毫无问题地进行 API 调用。然而,在同一网络上的另一台计算机上,例如,在 上192.168.x.x,前端运行,但其 API 调用不再工作。
我尝试使用代理作为 next.js 但仍然不起作用。
前端:
export default function People({setPerson}:PeopleProps) {
const fetcher = async (url:string) => await axios.get(url).then((res) => res.data);
const { data, error, isLoading } = useSWR(`${process.env.NEXT_PUBLIC_API}/people`, fetcher);
if (error) return <div>"Failed to load..."</div>;
return (
<>
{isLoading? "Loading..." :data.map((person: Person) =>
<div key={person.id}> {person.name} </div>)}
</>
)
}
Run Code Online (Sandbox Code Playgroud)
Next.js 应用程序env.local在启动时加载该文件,其中包含:
NEXT_PUBLIC_API=http://locahost:20002
后端:
rom typing import List
from fastapi import …Run Code Online (Sandbox Code Playgroud)