jQuery –拒绝访问受限制的URI –解决方案

问题

此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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值