在和「不同源服务器」同源的html中设置:
<script src="utils.js"></script> /* utils里有封装的ajax $ */
<script type="text/javascript">
// 返回当前文档的服务器域名,我们还能去设置 document.domain
document.domain = 'jsplusplus.com';
</script>
在要获取数据的「与服务器不同源」的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);