Read a page "distance"

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
fabby
Forum Commoner
Posts: 62
Joined: Tue Feb 15, 2005 1:06 am
Location: Romania
Contact:

Read a page "distance"

Post by fabby »

Hi all!
I have a problem when i want to show a banner on a site.
So, on the site A, www.domainA.com/banner.php - is just a banner

On the site B, www.domainB.com, in the right side, i want to show the banner, that is generated on the site A.
I tried this with ajax, and the code that i have to insert on site B is:

Code: Select all

<script type="text/javascript" language="javascript" src="http://www.domainA.com/include/show_banner.js"></script>
<script>show_banner_content();</script>
<div id="msg_err_rapid_login"></div>
where in the file show_banner.js is the function show_banner_content that user ajax to show the banner, and the banner is inserted inside the div <div id="msg_err_rapid_login"></div>

When i tried this on localhost, is ok, but when i put this online, does not want to work!
Can you please tell me why?
Thanks!
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Read a page "distance"

Post by Jade »

Sounds like a permissions problem.
fabby
Forum Commoner
Posts: 62
Joined: Tue Feb 15, 2005 1:06 am
Location: Romania
Contact:

Re: Read a page "distance"

Post by fabby »

what kind of permision? to know what to verify ?
fabby
Forum Commoner
Posts: 62
Joined: Tue Feb 15, 2005 1:06 am
Location: Romania
Contact:

Re: Read a page "distance"

Post by fabby »

the file, show_banner.js contain the function:

Code: Select all

function show_banner_content(){
	id_camp_err='msg_err_rapid_login';
	
	xmlHttp=GetXmlHttpObject();
	xmlHttp.open('post','www.domainA.com/banner.php');
	xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlHttp.onreadystatechange=insertError;
	xmlHttp.send('submit_form=da');
	setTimeout(show_banner_content,"3000");
	}
function insertError() 
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById(id_camp_err).innerHTML=xmlHttp.responseText 
	
	} 
} 
So, the problem i think is because can acces the function show_banner from "distance" or this don't work: xmlHttp.open('post','www.domainA.com/banner.php'); from "distance".

What do you think?
Thanks.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Read a page "distance"

Post by Weirdan »

XHR (XML Http Request) is limited to accessing urls only from the domain it runs on (not the domain the script originates from). So script running in the domainB context can't make requests to domainA urls.

You might get better results with JSONP + pointing 'src' attribute of the <script> tag to the remote endpoint. This works with GET requests only though, and that banner.php would likely need to be modified.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Read a page "distance"

Post by Apollo »

Weirdan wrote:XHR (XML Http Request) is limited to accessing urls only from the domain it runs on (not the domain the script originates from). So script running in the domainB context can't make requests to domainA urls.
Correct, and therefore a workaround could be to request domainB.com/dummy.php instead of domainA.com/banner.php in your ajax code, and dummy.php is simply a forwarding script that calls (and returns the result of) domainA.com/banner.php with the specified parameters.
fabby
Forum Commoner
Posts: 62
Joined: Tue Feb 15, 2005 1:06 am
Location: Romania
Contact:

Re: Read a page "distance"

Post by fabby »

Thank for the replays.
Do you know any examples to look how to do?
Post Reply