跨域解决方案

  1. 在讲解决跨域解决方案之前,我们需要了解什么是跨域,在什么情况下会跨域,跨域解决的是什么问题?

一、跨域,是指浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对JavaScript实施的安全限制。
同源策略限制了一下行为:
Cookie、LocalStorage 和 IndexDB 无法读取
DOM 和 JS 对象无法获取
Ajax请求发送不出去
二、跨域场景
所谓的同源是指,域名、协议、端口均为相同。

http://www.baianinfo.com/index.html 调用   http://www.baianinfo.com/server.php  非跨域

http://www.baianinfo.com/index.html 调用   http://www.baianinfo.cn/server.php  跨域,主域不同

http://abc.baianinfo.com/index.html 调用   http://def.baianinfo.com/server.php  跨域,子域名不同

http://www.baianinfo.com:8080/index.html 调用   http://www.baianinfo.com/server.php  跨域,端口不同

https://www.baianinfo.com/index.html 调用   http://www.baianinfo.com/server.php  跨域,协议不同

localhost   调用 127.0.0.1 跨域

2.跨域的解决方案。

  1. jsonp跨域 (一般只处理get请求)
  2. document.domain + iframe 跨域 (要求主域名相同)
  3. window.name + iframe 跨域
  4. location.hash + iframe 跨域
  5. postMessage跨域
  6. 跨域资源共享 CORS (主流的跨域解决方案)
  7. node 代理跨域
  8. 后端代理跨域
  9. nginx 代理跨域
server {
        listen       80;
        server_name  www.baidu.com;

        location /api {
            proxy_pass https://baidu.cn || http://0.0.0.0:8080 || https://1.2.3.4:8080;
            index  index.html index.htm index.jsp;
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容