天天看點

跨域通路的解決及問題

跨域這麼解決

a 網站某個頁面(ori.html)要 嵌套b網站中的詳情頁面。但是b網站中的詳情頁面(detail.html)的高度是動态的,要b網站告訴a網站。

解決方式:

a網站中 增加 一個cross.html頁面。裡面大概這麼寫

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8,9,10,11" />
<title>   </title>
<script type="text/javascript">
	
	function load(){
		var _heigth = getQueryStringRegExp("height");
		parent.parent.resizeHeight(_heigth);
		
	}
	
	/**
	 * 擷取iframe傳遞過來的參數
	 */
	function getQueryStringRegExp(name){  
	    var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");  
	    if (reg.test(location.href)) return decodeURIComponent(RegExp.$2.replace(/\+/g, " ")); return "";  
	}; 
</script>
</head>
<body οnlοad="load()">
</body>
</html>
           

a網站中的ori.html 裡放一個iframe。src位址是b網站中的detail.html,并且在參數中加上cross.html 的位址。

例如:src位址 http://b.com/detail.html?callBackUrl=http://a.com/cross.html

b網站中的detail.html 中增加一個iframe。頁面加載完成後,給iframe指派src位址為 傳遞過來的callBackUrl位址。

因為cross.html中寫的是parent.parent.方法。 是以實際上還是通路的他自己網站的内容。就沒有跨域了。

測試環境使用沒問題。

但是正式環境上有問題。提示401,發現response的頁面是b頁網站中的登入頁面。。。

不知道怎麼回事,cross.html不去a網站,而是在b網站找cross.html。經過搜尋發現,可能是spring-security導緻的。

http://blog.csdn.net/princeluan/article/details/73268637 但是我使用的網站中是spring3.x的,配置方式不一樣,也不知道具體怎麼配置。

打算通過nginx的配置來解決https://stackoverflow.com/questions/30731290/how-to-set-x-frame-options-allow-from-in-nginx-correctly

但是好像chorme浏覽器不支援某種配置。上篇文章中提到:Chrome does not support 

allow-from

另外參考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy

單獨通路對應的cross.html頁面是ok的,但是在detail.html中的iframe通路就不行。跳轉的是detail.html所在網站同名的頁面裡去了。經過觀察發現,請求都是一樣,隻不過一個有refer,一個沒有refer。沒有refer的正常。于是,在對應的b網站的cross.html中加入了location.replace(this.location.href) . 這樣變相的去掉了refer。就行了。

繼續閱讀