前端常见跨域解决方案


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

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM