2014-04-29 3 views
9

하위 도메인 내의 iframe에 액세스하려고하고 교차 도메인 오류가 발생합니다. 여기

가 .mydomain.com을/iframe_test.html 예의 코드 :동일한 도메인을 가진 iframe에 대해 Access-Control-Allow-Origin이 작동하지 않습니다.

<html> 
<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
</head> 
<body> 
    <iframe src="http://example2.mydomain.com/welcome.php" width="1000" height="600"></iframe> 
    <script> 
     $(document).ready(function() 
     { 
      setTimeout(function(){ 
       $('#innerdiv',$('iframe').contents()).hide(); 
      },5000); 
     }); 
    </script> 
</body> 
</html> 



그리고 여기가 의 부호 인 예 2 .mydomain.com을/welcome.php :

<?php 
header("Access-Control-Allow-Origin: " . "*"); 
?> 
<html> 
<head> 

</head> 
<body> 
    <div id="innerdiv"> 
     hello 
    </div> 
</body> 
</html> 



. 라인 $ (. '#의 innerdiv', $ ('iframe이')의 내용()) 숨기기()이 실행되면 다음과 같은 오류가 발생합니다

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://example.mydomain.com" from accessing a frame with origin "http://example2.mydomain.com". Protocols, domains, and ports must match. 


내가 피들러와 함께 확인하는 welcome.php의 응답으로 Access-Control-Allow-Origin 헤더가 실제로 반환되었습니다.

하위 도메인 내의 iframe 내용에 액세스 할 수 있습니까?

답변

15

Access-Control-Allow-Origin은 XHR에만 사용됩니다.

Same Origin Policy이 필요합니다.

페이지에 document.domain = 'example.com'을 추가해야합니다.

+0

해당 문서를 어디에 추가해야합니까? 도메인 항목은 무엇입니까? 조금 더 자세히 설명해 주시겠습니까? 고맙습니다. –

+0

@SaeedNeamati 두 페이지 모두를 script 태그에 추가해야합니다. 기본적으로 메인 페이지와 iframe 페이지는 ''을 설정해야합니다. –

+7

이 옵션이 작동하지 않습니다.'잡히지 않음 SecurityError : '문서'에 '도메인'속성을 설정하지 못했습니다. ':'example.com '은' '의 접미사가 아닙니다. –