简体   繁体   English

当所有文件都在本地位于同一目录中时,为什么会出现同源策略错误?

[英]Why am I getting a Same-origin policy error while all files are locally in the same directory?

I've got a main html file which loads another html file in the same directory through an iframe. 我有一个主要的html文件,该文件通过iframe加载了同一目录中的另一个html文件。 There is a button on the main page which targets the html file from the iframe. 主页上有一个按钮,用于定位来自iframe的html文件。 It's using this function: 它正在使用此功能:

    <script>
function myFunction() {
    var x = document.getElementsByTagName("iframe")[0].contentWindow;
    x.exportRoot.gotoAndPlay(2);
}
</script>

Live it works like intended: when I click the button it starts the Animate CC animation from within the iframe. Live的工作原理与预期的一样:当我单击按钮时,它将从iframe中启动Animate CC动画。 When I view the file locally however I get this error message in the console window: 但是,当我在本地查看文件时,会在控制台窗口中收到以下错误消息:

SecurityError (DOM Exception 18): Blocked a frame with origin "null" from accessing a frame with origin "null". SecurityError(DOM异常18):阻止了源为“ null”的帧访问源为“ null”的帧。 Protocols, domains, and ports must match. 协议,域和端口必须匹配。

I've read this has to do with same-origin policy. 我读过这与同源政策有关。 Which would make sense if the main page was at another domain as the iframe page. 如果主页与iframe页面位于另一个域,那将很有意义。 But both html file are locally in the same directory. 但是两个html文件都位于本地同一目录中。

My browsers (tried both Safari and Chrome) nonetheless see that as two different domains? 我的浏览器(同时尝试Safari和Chrome)都将其视为两个不同的域? Is there something I can change to make it work locally too? 我是否可以更改某些内容以使其在本地工作?

to do dev locally you should use a simple web server. 要在本地进行开发,您应该使用简单的Web服务器。 it will take you less than 2 minutes to set up. 只需不到2分钟即可完成设置。 there are a bunch here 这里有一堆

What is a faster alternative to Python's http.server (or SimpleHTTPServer)? 有什么比Python的http.server(或SimpleHTTPServer)更快的替代方法?

do NOT disable web security in your broswer. 不要在浏览器中禁用网络安全。 One forgetful click and your machine may be owned. 一键忘记,您的机器可能就会归自己所有。 Even if you think the risk is low why take any risk when the proper way of using a simple web server is so easy? 即使您认为风险很低,当使用简单的Web服务器的正确方法如此简单时,为什么还要冒险?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM