上一篇七種跨域方法【1.CROS篇】主要解決的是異域之間的傳值 這里主要解決的是子域與父域之間的傳值 問題描述: 現有父域:http://b.com/b.com.html 要向子域:http://a.b.com/a.b.com.html獲取數據 怎么辦? 將document.domain = 'b.com';都設置為父域即可
如果不知道如何配置虛擬主機?
http://blog.csdn.net/super_yang_android/article/details/53991982
父域:http://b.com/b.com.html內容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
document.domain = 'b.com';
var ifr = document.createElement('iframe');
ifr.src = 'http://a.b.com/a.b.com.html';
ifr.style.display = 'none';
document.body.appendChild(ifr);
ifr.onload = function(){
var doc = ifr.contentDocument || ifr.contentWindow.document;
// 這里操作DOM
var oUl = doc.getElementById('ul1');
alert(oUl.innerHTML);
ifr.onload = null;
};
</script>
</body>
</html>
子域:http://a.b.com/a.b.com.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
document.domain = 'b.com';
</script>
<ul id="ul1">我是子域a.b.com中的UL</ul>
</body>
</html>