主要是用到了egg-cors這個包蛹找,再進行配置一下就ok了
步驟一:
# 下載 egg-cors 包
npm i egg-cors --save
步驟二:
# 在plugin.js中設(shè)置開啟cors
exports.cors = {
enable: true,
package: 'egg-cors',
};
步驟三:
# 在config.{env}.js中配置零院,注意配置覆蓋的問題
config.security = {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: '*'
};
config.cors = {
origin:'*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};
或者下面的材蹬,步驟都一樣,放出的白名單不一樣而已
1.下載 egg-cors 包
npm i egg-cors --save
2.在plugin.js中設(shè)置開啟cors
exports.cors = {
enable: true,
package: 'egg-cors',
};
3.在config.default.js中配置
// add your config here
config.middleware = [];
//多出來的配置==========
config.security = {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ['http://localhost:8080']
};
config.cors = {
origin:'*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};