腾讯网络管理 發表於 2019-10-22 23:16:00

angular之跨域

<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong><span style="font-size: 16px">一、什么是跨域?</span></strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">&nbsp; &nbsp; 跨域是指一个域下的文档或者脚本去请求另一个域下的资源。(广义)</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">&nbsp; &nbsp; 广义的跨域:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">&nbsp; &nbsp;<strong> 1、资源跳转:</strong>链接跳转、重定向、表单提交。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">&nbsp; &nbsp;<strong> 2、资源嵌入:</strong>&lt;link&gt;、&lt;script&gt;、&lt;img&gt;、&lt;frame&gt;等dom标签;还有background:url()、</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @font-face()等文件外链css。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020190445416-1266846696.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp;<span style="font-size: 16px">&nbsp; <strong>3、脚本请求</strong></span><strong>:</strong>js发起的ajax请求、dom和js对象的跨域操作等。&nbsp;</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; 狭义的跨域:是由浏览器“同源策略”限制的一类请求场景。</span></p>
<p><strong><span style="font-family: &quot;Microsoft YaHei&quot;">二、同源策略</span></strong></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; <strong>同源策略/SOP(same origin policy)是一种约定,由Netscape公司1995年引入浏览器,它是浏览器最核心,也是最基本的安全功能,如果缺少了同源策略,浏览器很容易受到XSS、CSFR等攻击。</strong></span></p>
<p><strong><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; 所谓的同源是指“协议+域名+端口”三者相同,即便两个不同的域名指向同一个IP地址,也非同源。</span></strong></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; <strong>同源策略限制以下几种行为:</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; 1、Cookie、LocalSrorage、IndexDB无法读取。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; 2、DOM和JS对象无法获得。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; 3、AJAX请求不能发送。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;<strong> &nbsp; 常见跨域场景:</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020214254551-889860530.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;<strong>&nbsp;三、跨域解决方案</strong></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-size: 15px">-- 通过jsonp跨域</span></strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- document.domain + iframe跨域</strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- location.hash + iframe</strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- window.name + iframe跨域</strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- postMessage跨域</strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- 跨域资源共享(CORS)</strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- nginx代理跨域</strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- nodejs中间件代理跨域</strong></span><br><span style="font-size: 15px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- WebSocket协议跨域</strong></span></p>
<p>&nbsp;</p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; <strong>1、通过jsonp跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; 通常为了减轻Web服务器的负载、我们把js、css、img等静态资源分离到另一台独立域名的服务器上,在html页面中再通过相应的标签从不同域名下加载静态资源而被浏览器允许,也就是资源嵌入的方式(创建&lt;script&gt;标签的方式)</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; (1)原生:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020220800301-1936758156.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;服务端返回:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020220959849-1794332810.png" alt="" width="684" height="46"></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;(2)、jquery ajax实现:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020221147219-1327339094.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; (3)、angular实现:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020221758360-1072858543.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(4)vue实现:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020221929755-1489321671.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;后台node.js代码实现:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020222127789-168407902.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp;<strong>2、document.domain+iframe跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; 该方式仅限于主域相同,子域不相同的跨域应用场景。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; 实现原理:两个页面都通过js强制设置document.domain为基础生成,就实现了同域。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; (1)、父窗口:http://www.domain.com/a.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020222604420-1949748671.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;(2)、子窗口:http://child.domain.com/b.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020222705942-148364918.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp;<strong> &nbsp;3、location.hash+iframe跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>实现原理:</strong>a和b跨域相互通信,通过中间页c来实现。3个页面,不同域之间利用iframe的location.hash传值,相互域之间直接js访问来通信。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<strong>具体实现:</strong>A域:a.html -&gt; B域:b.html -&gt; A域:c.html,a与b不同域只能通过hash值单向通信,b与c也不同域也只能单向通信,但c与a同域,所以c可通过parent.parent访问a页面所有对象。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; (1)、a.html :&nbsp;http://www.domain1.com/a.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><code><span class="xml">&nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020223556573-1031556421.png" alt=""></span></code></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp;(2)、b.html:http://www.domain2.com/b.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020223712594-863681961.png" alt="" width="709" height="200"></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; (3)、c.html:http://www.domain1.com/c.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191020223906076-914986624.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp;<strong> 4、window.name+iframe跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.name属性的独特之处:name(大小限制2M)值在不同的页面(甚至不同域名)加载后依旧存在。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;(1)、a.html&nbsp; &nbsp;http://www.domain1.com/a.html</span></p>
<div class="cnblogs_code" style="width: 615px; margin: 5px 25px">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">var</span> proxy = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(url, callback) {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 0, 255, 1)">var</span> state = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>   <span style="color: rgba(0, 0, 255, 1)">var</span> iframe = document.createElement('iframe'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载跨域页面</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>   iframe.src =<span style="color: rgba(0, 0, 0, 1)"> url;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> onload事件会触发2次,第1次加载跨域页,并留存数据于window.name</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>   iframe.onload = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
</span><span style="color: rgba(0, 128, 128, 1)">10</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (state === 1<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">11</span>             <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第2次onload(同域proxy页)成功后,读取同域window.name中数据</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">            callback(iframe.contentWindow.name);
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">            destoryFrame();
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span>         } <span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (state === 0<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">16</span>             <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第1次onload(跨域页)成功后,切换到同域代理页面</span>
<span style="color: rgba(0, 128, 128, 1)">17</span>             iframe.contentWindow.location = 'http://www.domain1.com/proxy.html'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">18</span>             state = 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 0, 1)">    };
</span><span style="color: rgba(0, 128, 128, 1)">21</span>
<span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 0, 1)">    document.body.appendChild(iframe);
</span><span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取数据以后销毁这个iframe,释放内存;这也保证了安全(不被其他域frame js访问)</span>
<span style="color: rgba(0, 128, 128, 1)">25</span>   <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> destoryFrame() {
</span><span style="color: rgba(0, 128, 128, 1)">26</span>         iframe.contentWindow.document.write(''<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)">      iframe.contentWindow.close();
</span><span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">      document.body.removeChild(iframe);
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)">};
</span><span style="color: rgba(0, 128, 128, 1)">31</span>
<span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 请求跨域b页面数据</span>
<span style="color: rgba(0, 128, 128, 1)">33</span> proxy('http://www.domain2.com/b.html', <span style="color: rgba(0, 0, 255, 1); width: 100px">function</span><span style="color: rgba(0, 0, 0, 1)">(data){
</span><span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 0, 0, 1)">    alert(data);
</span><span style="color: rgba(0, 128, 128, 1)">35</span> });</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; (2)、proxy.html&nbsp; &nbsp;&nbsp;http://www.domain1.com/proxy....</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp;中间代理页,与a.html同域,内容为空即可。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; (3)、b.html&nbsp; &nbsp;&nbsp;http://www.domain2.com/b.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191021205852063-959840543.png" alt="" width="630" height="73"></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp;注意:通过iframe的src属性有外域转向本地域,跨域数据即由iframe的window.name从外域传递到本地域。这个就巧妙的绕过了浏览器的跨域访问限制,同时它又是安全操作。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; <strong>&nbsp; 5、postMssage跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postMessage是HTML5 XMLHttpRequest Level 2中的API,且是为数不多可以跨域操作的window属性之一,它可以解决以下方面的问题。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; (1)页面和其他打开的新窗口的数据传递。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; (2)多窗口之间消息传递。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; (3)页面与嵌套的iframe消息传递。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; (4)上面三个场景的跨域数据出传递。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 用法:postMessage(data,origin)方法接受两个参数。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:html5规范支持任意基本类型或可复制的对象,但部分浏览器只支持字符串,所以传参时最好用JSON.stringfy()序列化。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; origin:协议+主机+端口号,也可以设置“ * ”,表示可以传递给任意窗口,如果要制定和当前窗口同源的话设置为“/”。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ① a.html&nbsp; &nbsp;http://www.domain1.com/a.html</span></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">&lt;iframe id="iframe" src="http://www.domain2.com/b.html" style="display:none;"&gt;&lt;/iframe&gt;
&lt;script&gt;      
    <span style="color: rgba(0, 0, 255, 1)">var</span> iframe = document.getElementById('iframe'<span style="color: rgba(0, 0, 0, 1)">);
    iframe.onload </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> data =<span style="color: rgba(0, 0, 0, 1)"> {
            name: </span>'aym'<span style="color: rgba(0, 0, 0, 1)">
      };
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 向domain2传送跨域数据</span>
      iframe.contentWindow.postMessage(JSON.stringify(data), 'http://www.domain2.com'<span style="color: rgba(0, 0, 0, 1)">);
    };

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接受domain2返回数据</span>
    window.addEventListener('message', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e) {
      alert(</span>'data from domain2 ---&gt; ' +<span style="color: rgba(0, 0, 0, 1)"> e.data);
    }, </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
</span>&lt;/script&gt;</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;② b.html&nbsp; &nbsp;&nbsp;http://www.domain2.com/b.html</span></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">&lt;script&gt;
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收domain1的数据</span>
    window.addEventListener('message', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e) {
      alert(</span>'data from domain1 ---&gt; ' +<span style="color: rgba(0, 0, 0, 1)"> e.data);

      </span><span style="color: rgba(0, 0, 255, 1)">var</span> data =<span style="color: rgba(0, 0, 0, 1)"> JSON.parse(e.data);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (data) {
            data.number </span>= 16<span style="color: rgba(0, 0, 0, 1)">;

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 处理后再发回domain1</span>
            window.parent.postMessage(JSON.stringify(data), 'http://www.domain1.com'<span style="color: rgba(0, 0, 0, 1)">);
      }
    }, </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
</span>&lt;/script&gt;</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp;<strong> 6、跨域资源共享(CORS)</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>普通跨域请求:只</strong>服务端设置Access - Control - Allow - Origin即可,前段不用设置,如果要带cookie请求:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;前后端都需要设置。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; <strong>&nbsp;需要注意的是:</strong>由于同源策略的限制,所读取的cookie为跨域请求接口所在域的cookie,而非当前页。如果想实现当前页cookie的写入可以参考nginx反向代理中设置proxy_cookie_domain和NodeJs中间件代理中cookieDomainRewite参数的设置。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;目前,所有浏览器都支持该功能(IE8+,IE8/9需要使用XDomainRequest对象来支持CORS),CORS也应成为主流的跨域解决方案。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; (1)前端设置</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ① 原生ajax</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191021221331813-1254827831.png" alt="" width="684" height="50"></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 实例代码:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191021221435923-1283016865.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;② Jquery ajax</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191021221613245-183173303.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;③ vue框架</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a、axios设置:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191021221740252-352765230.png" alt="" width="649" height="43"></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b、vue-resource设置:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191021221837490-1867432174.png" alt="" width="572" height="40"></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(2)服务端设置</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 若后端设置成功,前端浏览器控制台则不会出现跨域报错信息,繁殖说明没有成功。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;① Java后台:&nbsp;</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; <img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191021222436441-391881076.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ② NodeJs后台实例</span></p>
<div class="cnblogs_code" style="width: 815px">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="color: rgba(0, 0, 255, 1)">var</span> http = require('http'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> server =<span style="color: rgba(0, 0, 0, 1)"> http.createServer();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> qs = require('querystring'<span style="color: rgba(0, 0, 0, 1)">);

server.on(</span>'request', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(req, res) {
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> postData = ''<span style="color: rgba(0, 0, 0, 1)">;

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据块接收中</span>
    req.addListener('data', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(chunk) {
      postData </span>+=<span style="color: rgba(0, 0, 0, 1)"> chunk;
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据接收完毕</span>
    req.addListener('end', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      postData </span>=<span style="color: rgba(0, 0, 0, 1)"> qs.parse(postData);

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 跨域后台设置</span>
      res.writeHead(200<span style="color: rgba(0, 0, 0, 1)">, {
            </span>'Access-Control-Allow-Credentials': 'true',   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 后端允许发送Cookie</span>
            'Access-Control-Allow-Origin': 'http://www.domain1.com',    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 允许访问的域(协议+域名+端口)</span>
            <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
             * 此处设置的cookie还是domain2的而非domain1,因为后端也不能跨域写cookie(nginx反向代理可以实现),
             * 但只要domain2中写入一次cookie认证,后面的跨域接口都能从domain2中获取cookie,从而实现所有的接口都能跨域访问
             </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
            'Set-Cookie': 'l=a123456;Path=/;Domain=www.domain2.com;HttpOnly'<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> HttpOnly的作用是让js无法读取cookie</span>
<span style="color: rgba(0, 0, 0, 1)">      });

      res.write(JSON.stringify(postData));
      res.end();
    });
});

server.listen(</span>'8080'<span style="color: rgba(0, 0, 0, 1)">);
console.log(</span>'Server is running at port 8080...');</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp;&nbsp;<strong>7、nginx代理跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>&nbsp; &nbsp; &nbsp; &nbsp; (1)nginx配置解决iconfont跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</strong>浏览器跨域访问js、css、img等常规静态资源被同源策略许可,但是iconfont字体文件(eot|otf|ttf|woff|svg)例外,此时可在 nginx的静态资源服务器中加入以下配置。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1513086/201910/1513086-20191022223605353-1877007220.png" alt=""></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>(2)nginx反向代理接口跨域</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 跨域原理:</strong>同源策略是浏览器的安全策略,不是HTTP协议的一部分。服务器端滴哦阿勇HTTP接口只是使用HTTP协议,不会执 行JS脚本,不需要同源策略,也就不存在跨域问题。</span></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>实现思路:</strong>通过nginx配置一个代理服务器(域名与domain1相同,端口不同)做跳板机,反向代理做domain2接口,并且可以 顺便修改cookie中domain信息,方便当前域cookie写入,实现跨域登录。</span></span></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>nginx具体配置:</strong></span></span></span></span></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="color: rgba(0, 0, 0, 1)">#proxy服务器
server {
    listen       </span>81<span style="color: rgba(0, 0, 0, 1)">;
    server_namewww.domain1.com;

    location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
      proxy_pass   http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.domain2.com:8080;#反向代理</span>
<span style="color: rgba(0, 0, 0, 1)">      proxy_cookie_domain www.domain2.com www.domain1.com; #修改cookie里域名
      indexindex.html index.htm;

      # 当用webpack</span>-dev-<span style="color: rgba(0, 0, 0, 1)">server等中间件代理接口访问nignx时,此时无浏览器参与,故没有同源限制,下面的跨域配置可不启用
      add_header Access</span>-Control-Allow-Origin http:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.domain1.com;#当前端只跨域不带cookie时,可为*</span>
      add_header Access-Control-Allow-Credentials <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    }
}</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong> &nbsp; &nbsp; &nbsp;前端代码示例:</strong></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> xhr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XMLHttpRequest();

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 前端开关:浏览器是否读写cookie</span>
xhr.withCredentials = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 访问nginx中的代理服务器</span>
xhr.open('get', 'http://www.domain1.com:81/?user=admin', <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
xhr.send();</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>&nbsp; Node.js后台实例:</strong></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> http = require('http'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> server =<span style="color: rgba(0, 0, 0, 1)"> http.createServer();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> qs = require('querystring'<span style="color: rgba(0, 0, 0, 1)">);

server.on(</span>'request', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(req, res) {
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> params = qs.parse(req.url.substring(2<span style="color: rgba(0, 0, 0, 1)">));

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 向前台写cookie</span>
    res.writeHead(200<span style="color: rgba(0, 0, 0, 1)">, {
      </span>'Set-Cookie': 'l=a123456;Path=/;Domain=www.domain2.com;HttpOnly'   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> HttpOnly:脚本无法读取</span>
<span style="color: rgba(0, 0, 0, 1)">    });

    res.write(JSON.stringify(params));
    res.end();
});

server.listen(</span>'8080'<span style="color: rgba(0, 0, 0, 1)">);
console.log(</span>'Server is running at port 8080...');</pre>
</div>
<p>&nbsp; <strong>&nbsp; 8、NodeJs中间件代理跨域</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node中间件实现跨域代理,原理大致与nginx相同,都是通过启动一个代理服务器,实现数据的转发,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;也可与通过设置 cookieDomainRewrite参数修改响应头中cookie中域名,实现当前域的cookie写入,方便接口登录认证。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <strong>&nbsp; &nbsp;非vue框架的跨域(2次跨域):</strong></p>
<p><strong>   利用node + express + http-proxy-middleware搭建一个proxy服务器。</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>(1)前端代码示例:</strong></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> xhr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XMLHttpRequest();

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 前端开关:浏览器是否读写cookie</span>
xhr.withCredentials = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 访问http-proxy-middleware代理服务器</span>
xhr.open('get', 'http://www.domain1.com:3000/login?user=admin', <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
xhr.send();</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>(2)中间件服务器:</strong></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> express = require('express'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> proxy = require('http-proxy-middleware'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> app =<span style="color: rgba(0, 0, 0, 1)"> express();

app.use(</span>'/'<span style="color: rgba(0, 0, 0, 1)">, proxy({
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 代理跨域目标接口</span>
    target: 'http://www.domain2.com:8080'<span style="color: rgba(0, 0, 0, 1)">,
    changeOrigin: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改响应头信息,实现跨域并允许带cookie</span>
    onProxyRes: <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(proxyRes, req, res) {
      res.header(</span>'Access-Control-Allow-Origin', 'http://www.domain1.com'<span style="color: rgba(0, 0, 0, 1)">);
      res.header(</span>'Access-Control-Allow-Credentials', 'true'<span style="color: rgba(0, 0, 0, 1)">);
    },

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改响应信息中的cookie域名</span>
    cookieDomainRewrite: 'www.domain1.com'<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可以为false,表示不修改</span>
<span style="color: rgba(0, 0, 0, 1)">}));

app.listen(</span>3000<span style="color: rgba(0, 0, 0, 1)">);
console.log(</span>'Proxy server is listen at port 3000...');</pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>(3)NodeJs后台同6</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong> &nbsp;vue框架的跨域(1次跨域):</strong></p>
<p style="text-align: left">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;&nbsp;</span><span style="font-family: &quot;Microsoft YaHei&quot;">利用node + webpack + webpack-dev-server代理接口跨域。在开发环境下,由于vue渲染服务和接口代理服务都是</span></p>
<p style="text-align: left"><span style="font-family: &quot;Microsoft YaHei&quot;"><em id="__mceDel">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;webpack-dev- server同一个,所以页面与代理接口之间不再跨域,无须设置headers跨域信息了。</em></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;webpack.config.js部分配置:</strong></span></p>
<div class="cnblogs_code" style="width: 840px">
<pre>module.exports =<span style="color: rgba(0, 0, 0, 1)"> {
    entry: {},
    module: {},
    ...
    devServer: {
      historyApiFallback: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      proxy: [{
            context: </span>'/login'<span style="color: rgba(0, 0, 0, 1)">,
            target: </span>'http://www.domain2.com:8080',<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 代理跨域目标接口</span>
            changeOrigin: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
            secure: </span><span style="color: rgba(0, 0, 255, 1)">false</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当代理某些https服务报错时用</span>
            cookieDomainRewrite: 'www.domain1.com'<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可以为false,表示不修改</span>
<span style="color: rgba(0, 0, 0, 1)">      }],
      noInfo: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
    }
}</span></pre>
</div>
<p>&nbsp;<strong> &nbsp; 9、webSocket协议跨域</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</strong>webSocket protocol是HTML5一种新的协议,它实现了浏览器域服务器全双工通信,同时允许跨域通讯,<em id="__mceDel">是server push</em></p>
<p><em id="__mceDel">&nbsp; &nbsp; &nbsp; &nbsp; 技术的一种很好的实现。</em></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;原生WebSocket API使用起来不太方便,我们使用Sockie.io,它很好的封装了webSocket接口,提供了更简洁、灵活的接口,也对不支&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;持webSocket的浏览器提供了向下兼容。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<strong>(1)前端代码:</strong></p>
<div class="cnblogs_code" style="width: 840px">
<pre>&lt;div&gt;user input:&lt;input type="text"&gt;&lt;/div&gt;
&lt;script src="https://cdn.bootcss.com/socket.io/2.2.0/socket.io.js"&gt;&lt;/script&gt;
&lt;script&gt;
<span style="color: rgba(0, 0, 255, 1)">var</span> socket = io('http://www.domain2.com:8080'<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接成功处理</span>
socket.on('connect', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监听服务端消息</span>
    socket.on('message', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(msg) {
      console.log(</span>'data from server: ---&gt; ' +<span style="color: rgba(0, 0, 0, 1)"> msg);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监听服务端关闭</span>
    socket.on('disconnect', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      console.log(</span>'Server socket has closed.'<span style="color: rgba(0, 0, 0, 1)">);
    });
});

document.getElementsByTagName(</span>'input').onblur = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
    socket.send(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.value);
};
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp; &nbsp; &nbsp; <strong>(2)NodeJs Socket后台:</strong></p>
<div class="cnblogs_code" style="width: 840px">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> http = require('http'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> socket = require('socket.io'<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 启http服务</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> server = http.createServer(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(req, res) {
    res.writeHead(</span>200<span style="color: rgba(0, 0, 0, 1)">, {
      </span>'Content-type': 'text/html'<span style="color: rgba(0, 0, 0, 1)">
    });
    res.end();
});

server.listen(</span>'8080'<span style="color: rgba(0, 0, 0, 1)">);
console.log(</span>'Server is running at port 8080...'<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监听socket连接</span>
socket.listen(server).on('connection', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(client) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收信息</span>
    client.on('message', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(msg) {
      client.send(</span>'hello:' +<span style="color: rgba(0, 0, 0, 1)"> msg);
      console.log(</span>'data from client: ---&gt; ' +<span style="color: rgba(0, 0, 0, 1)"> msg);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 断开处理</span>
    client.on('disconnect', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      console.log(</span>'Client socket has closed.'<span style="color: rgba(0, 0, 0, 1)">);
    });
});</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>&nbsp; &nbsp; &nbsp;&nbsp;</strong></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp;&nbsp;</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></p><br><br>
来源:https://www.cnblogs.com/Holin/p/11710709.html
頁: [1]
查看完整版本: angular之跨域