问题
此jQuery错误消息是由加载跨域内容引起的。
Error: [Exception... "Access to restricted URI denied"
code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)"
这意味着您正在加载一些不属于您站点或不在您站点上的内容(不同的域名)。 请参阅此jQuery示例以按需加载跨域(yahoo.com)内容。
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<style type="text/css">
#content{
border:1px solid blue;
margin:16px;
padding:16px;
}
</style>
</head>
<body>
<div id="msg"></div>
<div id="content">
</div>
<br/>
<button id="load">Load yahoo.com</button>
<script type="text/javascript">
$('#load').click(function(){
$('#msg').text("Loading......");
$('#content').load("http://www.yahoo.com", function() {
$('#msg').text("");
});
});
</script>
</body>
</html>
但是,这将不起作用,当您单击“加载”按钮时,它只会执行任何操作,而只会提示“ 拒绝访问受限制的URI ”错误消息。 由于JavaScript的安全性限制,因此严格不允许加载跨域内容 。
解
这是一个肮脏的解决方法–使用服务器端语言获取跨域内容。 例如,创建一个名为“ proxy.php”的单行php文件。
proxy.php
<?php echo file_get_contents($_GET['url']);?>
在jQuery端,将load函数更改为
$('#load').click(function(){
$('#msg').text("Loading......");
$('#content').load("proxy.php?url=http://www.yahoo.com", function() {
$('#msg').text("");
});
});
</script>
现在,当您单击“加载”按钮时,它将按需将跨域(yahoo.com)内容加载到您的页面中。
翻译自: https://mkyong.com/jquery/jquery-access-to-restricted-uri-denied-solution/