前后端分离下,egg如何设置跨域

前端url是
http://localhost:3333/index.html#/detail这样的格式
访问后端,报跨域错误
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3333' is therefore not allowed access.
请问后端如何设置
config.default.js里设置白名单:
http://localhost:3333/index.html#/detail
仍然无效。百思不得姐,求教高手!

阅读 21.3k
3 个回答

不设置白名单,直接在egg跨域插件配置的origin里面指定访问域名并开启认证模式就好了

//config.js
config.cors = {
    origin:'http://ip:port',
    credentials: true,
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  };
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进