<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Iacovos Constantinou &#187; Web Development</title>
	<atom:link href="http://www.iacons.net/writing/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iacons.net</link>
	<description></description>
	<lastBuildDate>Tue, 14 Oct 2008 17:59:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AJAX Cross Domain Proxy</title>
		<link>http://www.iacons.net/writing/2007/08/02/ajax-cross-domain-proxy/</link>
		<comments>http://www.iacons.net/writing/2007/08/02/ajax-cross-domain-proxy/#comments</comments>
		<pubDate>Thu, 02 Aug 2007 23:25:53 +0000</pubDate>
		<dc:creator>Iacovos</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://www.iacons.net/writing/2007/08/02/ajax-cross-domain-proxy/</guid>
		<description><![CDATA[<!-- CVFILE: $('#target').load(
	'http://www.yourdomain.com/ajax-proxy.php', {
		csurl: 'http://www.cross-domain.com/',
		param1: value1, param2: value2
	}
); --><!-- CVFILE: $valid_requests = array(
  'http://www.domainA.com/',
  'http://www.domainB.com/path-to-services/service-a'
); -->It is well known that cross domain AJAX requests (XMLHTTPRequest) are not permitted due to security reasons. Numerous workarounds exist such as cross domain JSON and Flash but some of them are not suitable for every single case. For instance, cross domain JSON assumes that remote server is able not only to serve JSON but [...]]]></description>
			<content:encoded><![CDATA[<!-- CVFILE: $('#target').load(
	'http://www.yourdomain.com/ajax-proxy.php', {
		csurl: 'http://www.cross-domain.com/',
		param1: value1, param2: value2
	}
); --><!-- CVFILE: $valid_requests = array(
  'http://www.domainA.com/',
  'http://www.domainB.com/path-to-services/service-a'
); --><p>It is well known that cross domain <acronym title="Asynchronous JavaScript and XML">AJAX</acronym> requests (XMLHTTPRequest) are not permitted due to security reasons. Numerous workarounds exist such as cross domain <acronym title="JavaScript Object Notation">JSON</acronym> and Flash but some of them are not suitable for every single case. For instance, cross domain <acronym title="JavaScript Object Notation">JSON</acronym> assumes that remote server is able not only to serve <acronym title="JavaScript Object Notation">JSON</acronym> but to include a call to the specified function (the <em>callback</em> function) as well. On the other hand, Flash method assumes that&#8230; well, that Flash is enabled!</p>
<p>An interesting approach is presented by <a href="ttp://www.themaninblue.com/">Cameron Adams</a> in his great article <a href="http://www.thinkvitamin.com/features/ajax/go-forth-and-api">Go forth and API</a>. Cameron suggests to take advantage of <code>mod_rewrite</code> or <code>mod_proxy</code> module in Apache in order to redirect our calls in external domains; a simple but ingenious solution! However, the most common solution is the application proxy which is accompanied by some advantages outlined perfectly well by <a href="http://snook.ca/jonathan/about/">Jonathan Snook</a>:</p>
<blockquote cite="http://snook.ca/archives/javascript/cross_domain_aj/"><p> [...] you have more control over the entire lifecycle. You can parse the data from the remote server, do with it what you will before sending it back to the client. If anything fails along the way, you can handle it in your own way. And lastly, you can log all remote calls. With that you can track success, failure and popularity.</p>
<address><a href="http://snook.ca/archives/javascript/cross_domain_aj/" title="Cross Domain Ajax: a Quick Summary">Cross Domain Ajax: a Quick Summary</a></address>
</blockquote>
<p>Lately, I have developed an application proxy in PHP which I decided to publish. You can <a href="http://lab.iacons.net/ajax-proxy" title="See Ajax Cross Domain Proxy in action | Lab | Iacovos Constantinou">have a look at the demo</a> and of course <a href="http://stats.iacons.net/cse-track.php?request_type=download&amp;site_id=1&amp;request_url=http://lab.iacons.net/ajax-proxy/ajax-proxy.phps" title="Download Ajax Cross Domain Proxy | Lab | Iacovos Constantinou">download it</a>.</p>
<p><strong>How it works</strong>? All you have to do is to place the corresponding file in your web server. Whenever you want to make a cross domain request, just make a request to <code>http://www.yourdomain.com/ajax-proxy.php</code> and specify the cross domain URL in parameter <code>csurl</code>. Obviously, you can add more parameters according to your needs; note that the rest of the parameters will be used for the cross domain request. For example, if you are using jQuery:</p>
<ol class="codelist">
<li class="tab0 odd"><code>$('#target').load(</code></li>
<li class="tab1 even"><code>'http://www.yourdomain.com/ajax-proxy.php', {</code></li>
<li class="tab2 odd"><code>csurl: 'http://www.cross-domain.com/',</code></li>
<li class="tab2 even"><code>param1: value1, param2: value2</code></li>
<li class="tab1 odd"><code>}</code></li>
<li class="tab0 even"><code>);</code></li>
</ol>
<p>It&#8217;s worth mentioning that both <code>POST</code> and <code>GET</code> methods work, while headers were taken into consideration. That is to say, headers sent from browser to proxy are used for the cross domain request and vice versa. Finally, for security reasons you will need to define all the valid requests into the <code>ajax-proxy.php</code> file:</p>
<ol class="codelist">
<li class="tab0 odd"><code>$valid_requests = array(</code></li>
<li class="tab2 even"><code>'http://www.domainA.com/',</code></li>
<li class="tab2 odd"><code>'http://www.domainB.com/path-to-services/service-a'</code></li>
<li class="tab0 even"><code>);</code></li>
</ol>
<p>Please note that the script is released under a <a href="http://creativecommons.org/licenses/GPL/2.0/">CC-GNU GPL</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iacons.net/writing/2007/08/02/ajax-cross-domain-proxy/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>
