修改nuxt.config.ts:
export default defineNuxtConfig({ ..... //跨域处理 nitro: { devProxy: { "/api": { target: process.env.NUXT_KINGBAL_PROXY_PATH, // 目标后端API地址 changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ''), }, }, }, .....})
如上所述,直接在nuxt.config.ts中添加跨域处理部分即可~~~
正式环境的时候我们也需要服务器支持跨域,我们直接配置nginx即可:
server { listen 80; server_name www.178xyw.com; location / { proxy_pass http://localhost:6666/; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
其中 www.178xyw.com 需要变更为你的绑定域名