阻止了来自“null”起源的框架访问跨域框架 - Chrome

47

我是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>

以下是源文件

<!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>

1
你是在本地机器上运行还是托管在Web服务器上? - CodingIntrigue
4
默认情况下,Chrome不允许从本地硬盘加载的框架访问彼此的内容。如果这些框架在同一台Web服务器上,则可以访问。Chrome有一个命令行参数,可以暂时暂停此安全检查。 - jfriend00
1
请注意,此问题适用于“iframe”和“frame”。 - posfan12
4个回答

44

7
当我在本地服务器中尝试后,无法使其工作。出现了这个错误:blocked a frame of origin “http://127.0.0.1:8080” from accessing a cross-origin frame - chrome - rehman_00001
6
"Firefox 75.0" 似乎出现了相同的问题。 - Alex P.
2
@TaouBen,这可能不是正确的答案。它在6年前被回答并且是正确的。如果您有更新的答案,可以在此帖子中添加它,以便当前成员从中受益。谢谢。 - amulya349
2
@amulya349:不再是正确答案了。 - TaouBen
1
确保在设置 iframe 时添加以下内容以使其正常工作:<iframe sandbox="allow-scripts allow-same-origin" src="..."></iframe> - Gal Bracha
显示剩余3条评论

7
如果你使用Visual Studio Code,你可以安装一个名为"Live Server"的扩展。当我遇到同样的问题时,它对我很有帮助。

3

1

将以下代码保存为same_server_source.html,在同一文件夹中运行python -m http.server,然后浏览到http://localhost:8000/same_server_source.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View same server source</title>
</head>
<body>
    <iframe id="iframe" src="/" sandbox="allow-scripts allow-same-origin allow-modal"></iframe><br>
    <button onclick="alert(iframe.contentWindow.document.body.innerHTML)">View home source</button>
</body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接