项目跨域是指:本项目作为被访问方,由另一个项目对本项目发起fetch等动作获取数据页面数据
实验环境:
- next:
14.1.0
- react:
^18
配置文件:next.config.[mjs|js|ts]
假定原始范本内容:
/** @type {import('next').NextConfig} */
const nextConfig = {};
export default nextConfig;
项目跨域配置:
/** @type {import('next').NextConfig} */
const nextConfig = {
// @link https://nextjs.org/docs/app/api-reference/next-config-js/headers
async headers(){
return [
{
source: "/:path*",
headers: [
{ key: "Access-Control-Allow-Origin", value: "*" },
],
}
]
}
};
export default nextConfig;
至此重启项目即可;