Main Tutorials

jQuery – Access to restricted URI denied – Solution

Problem

This jQuery error message is caused by loading the cross domain content.


Error: [Exception... "Access to restricted URI denied"  
code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)"

It’s means you are loading some content that are not belong to or located at your site (different domain name). See this jQuery example to load the cross domain (yahoo.com) content on demand.


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

However, this will not work, when you click on the “load” button, it just do nothing but prompt a “Access to restricted URI denied” error message. Due to the JavaScript security constraints, it’s strictly not allow to load cross domain content.

Solution

Here’s a dirty workaround – Get the cross domain content with server side language. For example, create a one line php file named “proxy.php”.

proxy.php


<?php echo file_get_contents($_GET['url']);?>

In jQuery side, change the load function to


$('#load').click(function(){
	$('#msg').text("Loading......");
	$('#content').load("proxy.php?url=http://www.yahoo.com", function() {
 		$('#msg').text("");
	});
});
</script>

Now, when you clicked on the “load” button, it will load the cross domain (yahoo.com) content into your page on demand.

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
fusitaku
10 years ago

You can use YQL for cross domain call without server side coding.

http://developer.yahoo.com/yql/

Fujita
12 years ago

Man, you save lives! Code works great for cross domains!!

Lolo
12 years ago
Reply to  Fujita

Hello Mkyong 🙂
I followed your steps but I still have a problem : here is the message error :

Warning: file_get_contents(http://shoutcast.mixstream.net/contents/current.php?s=uk3-free) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /htdocs/public/test/stream/proxy.php on line 1

(see here : http://test.corporex.info/stream/infos.php)

In fact i’d like to have this page (http://shoutcast.mixstream.net/station/uk3-free:22302) on my site so i can change the background image because i don’t like it 😉

My site is on a shared server and i don’t have access to the php.ini

if it can help you, here is the phpinfo:

http://test.corporex.info/phpinfo.php

Any idea ?

Thank you for you help ! 🙂

Regards,

Laurent