Click here to Skip to main content
Click here to Skip to main content
Go to top

Cross Domain Request Pattern

, 1 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Exchange data between two domains using AJAX

I had a project where I needed to exchange data between two domains using AJAX. All major browsers don't allow cross-domain requests for security reasons. If you control both domains, you could use HTTP headers as described in Cross-Origin Resource Sharing. I tried it, and found that not all browsers support this standard. The good thing is that you can still do it by following this simple idea.

Consider two domains and Obviously, needs to get some data from

First, we call from a hidden iframe:

<iframe src="", 
style="width:0;height:0;border:0px solid #fff;">

function proxy_done(reply)
... process reply from

The 'style' attribute hides the iframe. The ‘proxy_done’ function is called when the reply is ready. The iframe element with is not allowed to communicate with the parent page ( We just need to redirect it back to


On, create request.html with something like the following.

<script language="javascript" type="text/javascript">
var reply = ... process the request and get reply
self.location.href="" + reply;

The script redirects the iframe along with the reply data back to a proxy page (see below) that resides at

At this time, the original iframe element on contains proxy_page.html with the reply from Because proxy_page.html is on it can now access its parent page. Now, we can call the 'proxy_done' function, and pass it the reply.

echo '<script> var reply="'.$_GET['reply'].'"; </script>';

That's it. Most likely you'd want to be able to call dynamically from your JavaScript application at To do that, just create the iframe element dynamically... document.createElement("iframe")... all this DOM stuff follows...


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


United States United States
No Biography provided

Comments and Discussions

QuestionDoes this solution works for IE6 browser? PinmemberMember 34228776-Dec-11 9:45 
GeneralMy vote of 3 PinmemberRoey C5-Dec-11 20:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by egladysh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid