Javascript 阻止原始框架“null”访问跨源框架 - chrome

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29983786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:25:54  来源:igfitidea点击:

blocked a frame of origin "null" from accessing a cross-origin frame - chrome

javascriptgoogle-chrome

提问by Matt Fenlon

I'm new to Javascript and am learning the basics via a textbook that focuses on its applications in IE 7+ and Firefox 2+. However, I am using Chrome and am getting the following error when running the program given in the book: "blocked a frame of origin 'null' from accessing a cross-origin frame." Can anyone tell me what is causing the error and how I can fix it? The two programs are below.

我是 Javascript 的新手,正在通过一本教科书学习基础知识,该教科书侧重于 IE 7+ 和 Firefox 2+ 中的应用程序。但是,我正在使用 Chrome,并且在运行书中给出的程序时出现以下错误:“阻止了一个原始框架 'null' 访问跨源框架。” 谁能告诉我是什么导致了错误以及如何修复它?这两个程序如下。

//This is the program being loaded into the browser
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>

<script type="text/javascript">

function calcFactorial(factorialNumber){
    var factorialResult = 1;
    for(;factorialNumber>0;factorialNumber--) factorialResult *= factorialNumber;
    return factorialResult;
}

</script>

</head>

<frameset cols="100%,*">
<frame name="fraCalcFactorial" src="calcfactorial.htm"/>
</frameset>

</html>

Below is the src file

下面是src文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>
<script type="text/javascript">
function butCalculate_onclick(){
    try{
        if (window.top.calcFactorial == null)
            throw "This page is not loaded within the correct frameset";
        if (document.form1.txtNum1.value == "")
            throw "!Please enter a value before you calculate its factorial";
        if (isNaN(document.form1.txtNum1.value))
            throw "!Please enter a valid number";
        if (document.form1.txtNum1.value < 0)
            throw "!Please enter a positive number";
    }
    catch(exception){
        if (typeof(exception) == "string"){
            if (exception.charAt(0) == "!"){
                alert(exception.substr(1));
                document.form1.txtNum1.focus();
                document.form1.txtNum1.select();
            }
            else alert(exception);
        }
        else alert("The following error occurred: " + exception.message);
    }
}
</script>
</head>
<body>
<form action="" name="form1">
    <input type="text" name="txtNum1" size="3" /> factorial is
    <input type="text" name="txtResult" size="25" /><br/>
    <input type="button" value="Calculate Factorial"
        name="butCalculate" onclick="butCalculate_onclick()" />
</form>
</body>
</html>

回答by amulya349

This happens because Chrome doesn't allow frames from your hard disk to access each others' content. Which, technically we term as Cross-origin request.

发生这种情况是因为 Chrome 不允许您硬盘中的帧访问彼此的内容。从技术上讲,我们称之为跨域请求。

Solution of the above problem is:

1. Either you host your webpage on a local web server. See the following link:
What is a faster alternative to Python's http.server (or SimpleHTTPServer)?

2. Use any other browser like Firefox

解决上述问题的方法是:

1. 要么将您的网页托管在本地 Web 服务器上。请参阅以下链接:
Python 的 http.server(或 SimpleHTTPServer)的更快替代方法是什么?

2. 使用任何其他浏览器,如 Firefox

回答by Alex Pandrea

If you don't want to use a local web server as suggested in the accepted answer you can run the browser with cross domain web security/ same origin policydisabled.

如果您不想按照接受的答案中的建议使用本地 Web 服务器,则可以使用cross domain web security/same origin policy禁用运行浏览器。

For Chrome:

对于

Disable same origin policy in Chrome

在 Chrome 中禁用同源策略

For Firefox:

对于火狐

Disable cross domain web security in Firefox

在 Firefox 中禁用跨域 Web 安全

https://addons.mozilla.org/en-US/firefox/addon/access-control-allow-origin/

https://addons.mozilla.org/en-US/firefox/addon/access-control-allow-origin/

Disable firefox same origin policy

禁用 Firefox 同源策略