跨域問題 Blocked a frame with origin "http://......" from accessing a cross-origin frame.

爲了輕鬆偷懶,不想從目的項目中開發目標項目中的頁面,但目的項目中須要獲取老項目中的頁面,這裏用了iframe跨域連接頁面出現了問題javascript

Blocked a frame with origin "http://......" from accessing a cross-origin frame.php

出現此報錯的緣由是由於在http://driversys.edaijia.cc 下操做 http://www.edaijia.cc   下的dom致使。html

項目框架是yii2,代碼以下,項目中是在看代碼中http://driversys.edaijia.cc 下操做的iframe鏈接指向是http://www.edaijia.cc   dom元素。java

 

//http://driversys.edaijia.cc 項目下代碼
<?php
Modal::begin([
    'id' => 'create-modal-article',
    'header' => '<h4 class="modal-title">選擇長文章</h4>',
    'footer' => '<a href="javascript:;" class="btn btn-info" id="confirm_article" >肯定</a><a href="javascript:;" class="btn btn-danger" id="close_article_window" >關閉</a>',
]);
$requestUrl = 'http://www.edaijia.cc/v2/index.php?r=newNoticePost/selection';
$js = <<<JS
$('#create-modal-article .modal-body').html('<iframe name="article_ifm" id="add_long_article" width="99%" height="100%" style="border:0px"></iframe>');
$("#add_long_article").attr("src", 'http://www.edaijia.cc/v2/index.php?r=newNoticePost/selection');

$('#create-modal-article').find('.modal-header').find('.close').hide();
JS;

解決方法:跨域

在父頁面添加監聽器,接收子頁面的值:yii2

window.addEventListener('message',function(e){框架

alert(e.data);dom

           },false);yii

 

在子頁面在要傳值的地方調用如下語句傳值:ide

window.parent.postMessage('值','http://www.....);

解決代碼:

//http://www.edaijia.cc項目代碼 

$("#new-notice-post-grid :radio").on('click', function () { //監聽radio選中,並把值傳遞
        window.parent.postMessage([$(this).attr('did'),$(this).val()],'http://driversys.edaijia.cc');
    })

 

//http://driversys.edaijia.cc  項目代碼,獲取傳遞的值
//長文章radio獲取
   window.addEventListener('message',function(e){
       window.did = e.data[0];
       window.title = e.data[1];

   },false);
相關文章
相關標籤/搜索