2015-02-02 13 views
18

Tôi đang gặp vấn đề bên dưới khi cố gắng nắm bắt các sự kiện nhấp của nút theo dõi G +.Uncaught SecurityError: Không đọc được thuộc tính 'contentDocument' từ 'HTMLIFrameElement': Bị chặn khung có nguồn gốc "https: // localhost"

Bảo mật không an toànLỗi: Không đọc được thuộc tính 'contentDocument' từ 'HTMLIFrameElement': Bị chặn khung có nguồn gốc "https://localhost" truy cập khung có nguồn gốc "https://apis.google.com". Giao thức, tên miền và cổng phải khớp.

+0

dupe có thể có của [của router SecurityError: Không thể đọc tài sản 'contentDocument' từ 'HTMLIFrameElement'] (http://stackoverflow.com/questions/ 26329519) hoặc [SecurityError: Không đọc được thuộc tính 'contentDocument' từ 'HTMLIFrameElement] (http://stackoverflow.com/questions/26657687) – hippietrail

Trả lời

5

Tôi đã tìm thấy một cuộc thảo luận tương tự, Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFram.

This issue fired when you try to call ajax to another domain, please check this article for more info about Same origin policy

Mozilla's Same Origin article

For fix this, you will need to add this code

document.domain = 'yourdomain.com' 

Từ bài viết riêng của mình:

A page may change its own origin with some limitations. A script can set the value of document.domain to a subset of the current domain. If it does so, the shorter domain is used for subsequent origin checks. For example, assume a script in the document at http://store.company.com/dir/other.html executes the following statement:

document.domain = "company.com"; 

After that statement executes, the page would pass the origin check with http://company.com/dir/page.html . However, by the same reasoning, company.com could not set document.domain to othercompany.com.

The port number is kept separately by the browser. Any call to the setter, including document.domain = document.domain causes the port number to be overwritten with null. Therefore one cannot make company.com:8080 talk to company.com by only setting document.domain = "company.com" in the first. It has to be set in both so that port numbers are both null.

+17

Tôi đã thử giải pháp của bạn, nhưng tôi nhận được thông báo: SecurityError: Không thể đặt' miền 'tài sản trên' Tài liệu ':' http://mimouni.info 'i không phải là hậu tố của 'localhost'. – Mimouni

0

Giải pháp của tôi tái cấu trúc IFRA tôi và có thể sử dụng ở góc cạnh. Khi chúng tôi xây dựng iframe, nó yêu cầu kiểm tra bảo mật gốc để sửa đổi nội dung iframe. Giải pháp này cho phép chúng tôi tạo lại nội dung khung nội tuyến vài lần.

HTML

<div id="iframecontainer"></div> 

JS

var content = "<h1>Content inside Iframe</h1>"; //desired content of iframe 
var iframecontainer = document.getElementById("iframecontainer"); 
iframecontainer.innerHTML ='<iframe id="threedsframe" width="%90" height="400px"></iframe>'; 
var iframe = iframecontainer.childNodes[0]; 
let doc = iframe.contentDocument || iframe.contentWindow; 
doc.open(); 
doc.write(content); 
doc.close(); 
Các vấn đề liên quan