Untitled

目标

Untitled

1. 🌈 服务器中转请求

Untitled

2. 🌈 设置基础域名 + iframe

Untitled

test.jsplusplus.com/index.html

在和「不同源服务器」同源的html中设置:

  1. 引入 utils.js 中的 ajax
  2. 设置 document.domain
<script src="utils.js"></script> /* utils里有封装的ajax $ */
<script type="text/javascript"> 
  // 返回当前文档的服务器域名,我们还能去设置 document.domain
  document.domain = 'jsplusplus.com';
</script>

test2.jsplusplus.com/index.html

在要获取数据的「与服务器不同源」的html中设置:

  1. document.domain
  2. 添加一个不可见的iframe,src为和「服务器」同源的html
  3. 使用和「服务器」同源的html中的 $ 去请求数据
document.domain = 'jsplusplus.com';

var iframe = document.createElement('iframe');
iframe.src = 'test.jsplusplus.com/index.html';
iframe.id = 'myIframe';
iframe.style.display = 'none';
iframe.onload = function() {
  var $$ = document.getElementById('myIframe').contentWindow.$;
  $$.post('<http://test.jsplusplus.com/get_courses1.php>', {
  	status: 1
  }, function(data) {
  	console.log(data);
  });
}

document.body.appendChild(iframe);