前后端分离
跨域问题
- 在前端项目,请求后台接口的URL指定为 http://epdcsiman.top/blog/xxx 或者http://ip/blog/xxx
出现错误 No 'Access-Control-Allow-Origin' header is present on the requested resource.
只可以在后台项目接口响应请求头中,增加Access-Control-Allow-Origin的header值为*,允许所有跨域请求。例如:spring项目中,拦截器中统一增加这个header
- 在前端项目,请求后台接口用localhost,例如:http://localhost/blog/xxx
出现错误 net::ERR_CONNECTION_REFUSED
不会走nginx代理,但服务器用localhost可以访问后台接口,在前端项目中用localhost不行。不知道原因,没有找到解决方法
- 在前端项目,请求后台接口的URL指定为 /blog/xxx
通过nginx代理转发到后台接口,这样不会有跨域问题(推荐使用这个方式)