Nextjs SSR获取原点



使用Nextjs getServerSideProps函数,我向API发出获取请求。我的API检查CORS的原始标头,但得到未定义的原始标头。为什么会发生这种情况,有办法解决吗?

在Next中,我在正常情况下从浏览器发出获取请求时会得到原始标头。只有当从Nextjs getServerSideProps通过服务器向API发出请求时,才会出现此问题。

我所做的是将Origin参数添加到fetch请求中的头对象中,并从.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);

最新更新