在NextJ中使用Axios时缺少CORS标题
我使用的是Nestjs后端带有NextJS Frontend,均分别托管。
nestjs后端
i在后端启用了Cors,如下所示:
app.enableCors({ credentials: true, origin: process.env.FRONTEND_URL });
使用 cors-test.cose-test.codehapp.codehappy.codehappy .dev 检查CORS标题,一切看起来都不错。所有标头都存在,访问控制 - 允许 - 原始
标题指向前端托管的正确域。
nextjs frontend
在NextJS前端我使用Axios向后端提出请求(与上面使用的完全相同的URL)。 Chrome中的要求缺少所有CORS标题。当需要HTTP请求时,下面的AXIOS实例将导入。
import Axios from 'axios';
const api = Axios.create({
baseURL: process.env.BACKENDURL,
withCredentials: true
});
export default api;
I'm using a NestJS backend with a NextJS frontend, both hosted seperately.
NestJS Backend
I enabled CORS in the backend as follows:
app.enableCors({ credentials: true, origin: process.env.FRONTEND_URL });
When using cors-test.codehappy.dev to check the CORS headers everything looks good. All headers are present and the access-control-allow-origin
header points to the right domain where the front-end is hosted on.
NextJS Frontend
On the NextJS frontend I'm using Axios to make request to the backend (the exact same url as used above).However, when creating a request the preflight request in Chrome is missing all CORS headers. The Axios instance below is imported when a HTTP request is needed.
import Axios from 'axios';
const api = Axios.create({
baseURL: process.env.BACKENDURL,
withCredentials: true
});
export default api;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
next.config.js
中in
next.config.js