我有一些看起来像这样的 javascript:
// https://secure.example.com
document.domain = "example.com";
window.myVar = "value";
// http://example.com
document.domain = "example.com";
var iframe = document.body.appendChild(document.createElement("iframe"));
iframe.onload = function () {
console.log(iframe.contentWindow.myVar);
}
iframe.src = "https://secure.example.com";
我正在尝试通过将 https://secure.example.com
加载到 http://example 中的 iframe 来访问
。请注意 myVar
。 comhttp
和 https
的用法。当两个域都使用 http
或 https
这有效,但现在我想让一个使用 http
而另一个使用 https
。我已经通过代码来验证 document.domain
是否设置正确。为什么会这样?
最佳答案
您的问题不是域或主机。你的问题是协议(protocol)
不能跨协议(protocol)设置document.domain
http
和 https
由于协议(protocol)原因不是同一来源,就像 example.com:80 由于端口原因与 example.com:8080 不是同一来源.
有很好的安全理由不允许混合使用 http 和 https
请阅读https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
关于javascript - document.domain 相同但仍然出现同源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895192/