Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决跨域问题 #18

Open
wolong438 opened this issue Jun 5, 2019 · 1 comment
Open

解决跨域问题 #18

wolong438 opened this issue Jun 5, 2019 · 1 comment

Comments

@wolong438
Copy link

前后端部署成功后,会发现前端页面获取后端数据时,会出现跨域问题。翻阅服务端的代码,是可以看到是有处理跨域的(header.set('Access-Control-Allow-Origin', '*')),但是并没有生效。
解决跨域问题的几种常规方式:后端header加跨域设置(没生效),前端使用代理(开发环境有效,生产环境无效),使用nginx代理(生产、开发环境都可以)。
出于仅仅研究项目的目的,此处采用前端代理的方式,如下:
打开前端neteasemusic/src/main.js文件,做出以下修改
//axios.defaults.baseURL = 'http://localhost:3000/v1/';
axios.defaults.baseURL = '/api';
打开前端neteasemusic/config/index.js文件,做出以下修改
proxyTable: {
"/api": {
target: "http://localhost:3000/v1/", //设置你调用的接口域名和端口号
changeOrigin: true, //跨域
pathRewrite: {
"^/api": "/"
}
}
}

最后,重启前端项目就行了,跨域问题解决了。

@sylbj23
Copy link

sylbj23 commented Jan 7, 2022

const cors = require("cors");
app.use(cors())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants