egg.js后台解决跨域问题

我们需要安装egg-cors插件:

	npm/cnpm install egg-cors --save
	yarn add egg-cors --save 

添加相关配置
在egg项目的config/plugins.js

	module.exports = {
		cors: {
			enable: true,
			package: 'egg-cors'
		}
	}

在egg项目的 config/config.default.js

	module.exports = {
		const config = exports = {}
		config.security = {
		    csrf: {
		      enable: false,
		      ignoreJSON: true
		    },
		    domainWhiteList: [ '*' ], // 白名单
	  	};
		config.cors = {
			origin: '*', // 如果不写origin则会按照白名单中的域名允许跨域 * 代表允许所有的域名进行跨域请求
			allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
		}
	}
	

最重要的一点是:在你请求的页面 不要忘了修改一下请求:

这样就可以了。。。。。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章