最近闲着没事研究一下关于http请求跨域,简单做个总结:
1.jsonp方式,但是这个方式存在一定的缺陷,首先,post请求无法使用,其次,需要前后端需要将数据转型为jsonp,对于http 标准json接口开发来说有点不适应
2.后端入口使用 header的方法允许跨域请求 Header("Access-Control-Allow-Origin", "*"),做到后端允许前端调用,但是这种配置会降低服务端的安全性,一般会要求前端一起配置此类参数,使用协定好的域名,杜绝协定外的域名请求
3.java:使用springboot注解 @CrossOrigin ,暴露接口允许跨域调用
4.nginx 支持跨域请求
location /{
add_header 'Access-Control-Allow-Origin' 'http://other.subdomain.com';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET';
... the rest of your configuration here ...
}