前端常见跨域解决方案

协议+域名+端有一个不同就会产生跨域。,常见跨域解决方案:

1、 通过jsonp跨域(jsonp缺点:只能实现get一种请求)
2、 document.domain + iframe跨域
3、 location.hash + iframe
4、 window.name + iframe跨域
5、 postMessage跨域
6、 跨域资源共享(CORS)  

  response.setHeader("Access-Control-Allow-Origin", "http://localhost:9105");(也可以设置为 * 表示该资源谁都可以用)

  response.setHeader("Access-Control-Allow-Credentials", "true");(若要带cookie请求:前后端都需要设置)

  springMVC的版本在4.2或以上版本,可以使用注解实现跨域,方法上添加:@CrossOrigin(origins="http://localhost:9105",allowCredentials="true")

7、 nginx代理跨域
8、 nodejs中间件代理跨域
9、 WebSocket协议跨域

 

 

原文:https://segmentfault.com/a/1190000011145364

posted @ 2019-07-12 18:01  苏大大的园子  阅读(1588)  评论(0编辑  收藏  举报