跨域打開頁面:Uncaught DOMException: Blocked a frame with origin


Uncaught DOMException: Blocked a frame with origin

使用postMessage()方法可以解決跨域傳值的問題
Api: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage

父頁面:

layer.open({
                skin: 'rocket',
                scrollbar: false,
                type: 2,
                title: 'test',
                shadeClose: true,
                area: ['50%', '50%'],
                content: url,//iframe的url
                btn:['保存'],
                btnAlign: 'c',
                yes: function(index, layero){
                    window[layero.find('iframe')[0]['name']].postMessage('addAndEdit', '*');
                    layer.close(index);
                },
                cancel: function(index, layero){
                    window[layero.find('iframe')[0]['name']].postMessage('addAndEdit', '*');
                    layer.close(index);
                }
            });
        

        function receiveMessage(event) {
            console.log(event.data)//取得子頁面傳回來的值
            var roomId = event.data;
            getSitUnitByRoomId(roomId)
        }

子頁面:

// 跨域發送消息
		window.addEventListener('message',function(e){
			var value = e.data;
			//返回方法向父頁面發送數據
			if (value != null && value == 'addAndEdit') {
				if (fangjianId) {
					parent.postMessage(fangjianId, "*")
				}
			}
		}, false);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM