Next.js SSR 获取源站数据

3
使用Nextjs的getServerSideProps函数,我向我的API发起了一次fetch请求。我的API检查CORS的来源头,但是获取到的来源头是未定义的。为什么会出现这种情况?是否有解决方法?
在Next中通过浏览器正常发起fetch请求时,我可以获取到来源头。但是当通过Nextjs的getServerSideProps从服务器向API发起请求时,才会出现这个问题。
1个回答

1
我所做的是在fetch请求中的header对象中添加Origin参数,并将.env文件中的url添加到其中,以便在生产环境中轻松更改。
  //Create fetch's options
  const options = {
     mode: 'cors',
     method: 'GET',
     credentials: 'include',
     headers: {
        'Content-Type': 'application/json',
        Origin: `${process.env.NEXT_PUBLIC_FRONT_URL}`,
     },
  };

  //Fetch data from the API
  const res = await fetch('changeForYouURL', options);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接