项目场景:
把前端打包后部署在服务器上,后端也部署在相同服务器,发现不能运行,报错如下:
Access to XMLHttpRequest at 'http://localhost:8080/' from origin
'http://47.94.213.111' has been blocked by CORS policy: The request
client is not a secure context and theresource is in more-private
address space `local`.
原因分析:
之前一直在本地运行前端与后端,后端地址为8080,因此前端访问后端的地址host-ip 如下:
http://localhost:8080
但是部署在服务器上,虽然前后端都在一个服务器上,但是也不能用localhost
解决方案:
把host-ip 地址改为:
http://47.94.213.111:8080
并把服务器的8080端口打开,即可访问。