eggjs cros 跨域问题
- Node Version:
- Egg Version:
- Plugin Name:
- Plugin Version:
- Platform:
- Mini Showcase Repository:
egg本地开发。在另一个系统中调用egg服务的接口时出现了跨域问题。引入了 egg-cors 后,依然有跨域问题,报了下面的错误, 哪位仁兄遇到过? Access to fetch at 'http://127.0.0.1:7001/test?' from origin 'http://localhost:9088' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
exports.security = {
domainWhiteList: [ 'http://localhost:4200' ],
};
Or:
// {app_root}/config/config.default.js
exports.cors = {
// {string|Function} origin: '*',
// {string|Array} allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};
If the origin is set, the plugin will follow it to set the Access-Control-Allow-Origin and ignore the security.domainWhiteList. Otherwise, the security.domainWhiteList which is default will take effect as described above.
- https://github.com/eggjs/egg-cors
config.cors = {
credentials: true,
origin: [ 'http://localhost:9088' ],
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
};
@thonatos 非常感谢你的回答。我之前也是像你发的那样设置的,还是会报跨域问题。 需要加上 credentials: true。 设置代码如上。domainWhiteList 可以不设置。
看了好多 issues 和文档都没有给出比较完整和具体的跨域配置方案 我尝试了只需要配置下面的内容可以成功:
// config/config.default.js
module.exports = appInfo => {
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1546505251052_7827';
// add your config here
config.middleware = [];
config.security = {
domainWhiteList: [ 'http://localhost:8899' ], // 没有配置的话,错误信息:404
csrf: {
enable: false, // 暂时禁用掉 csrf,错误信息:403 missing csrf token
},
}
return config;
};
安装 egg-cors
// config/plugin.js
exports.cors = {
enable: true,
package: 'egg-cors',
};
@104gogo
/ {app_root}/config/config.default.js
exports.cors = {
// 这里,支持函数
// {string|Function} origin: '*',
// {string|Array} allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};
嗯,光是跨域的话,我也觉得只需要配置 cors 就好了
@104gogo 404
看了好多 issues 和文档都没有给出比较完整和具体的跨域配置方案 我尝试了只需要配置下面的内容可以成功:
// config/config.default.js module.exports = appInfo => { const config = exports = {}; // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_1546505251052_7827'; // add your config here config.middleware = []; config.security = { domainWhiteList: [ 'http://localhost:8899' ], // 没有配置的话,错误信息:404 csrf: { enable: false, // 暂时禁用掉 csrf,错误信息:403 missing csrf token }, } return config; };安装 egg-cors
// config/plugin.js exports.cors = { enable: true, package: 'egg-cors', };
就这个可用!!!👍
看了好多 issues 和文档都没有给出比较完整和具体的跨域配置方案 我尝试了只需要配置下面的内容可以成功:
// config/config.default.js module.exports = appInfo => { const config = exports = {}; // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_1546505251052_7827'; // add your config here config.middleware = []; config.security = { domainWhiteList: [ 'http://localhost:8899' ], // 没有配置的话,错误信息:404 csrf: { enable: false, // 暂时禁用掉 csrf,错误信息:403 missing csrf token }, } return config; };安装 egg-cors
// config/plugin.js exports.cors = { enable: true, package: 'egg-cors', };
建议官方文档改成这个 @atian25
其实应该配置 origin 而不是 domainWhiteList
欢迎 PR
我的options预检请求就直接挂掉了,怎么解