# 什么是跨域

协议, 域名, 端口不一致 即为 跨域

# 您是如何解决跨域的

解决方案:

# (1). 通过jsonp跨域

原理:动态添加一个<script>标签,而script标签的src属性是没有跨域的限制的。

缺点: 只支持GET请求而不支持POST等其它类型的HTTP请求

<script type="text/javascript"> 
  function jsonpCallback(res) 
  { 
    alert(res.msg); 
  } 
</script> 
<script type="text/javascript"src="http://xxx.com/xx?jsonp=jsonpCallback"></script>
1
2
3
4
5
6
7

# (2). 跨域资源共享(CORS)

这是目前最常用的跨域解决方案

# (3). WebSocket

# (4). postMessage

# (5). nginx代理跨域

# (6). nodejs中间件代理跨域

# .......

上次更新: 2020/10/12 下午2:13:19