<?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>Orange is my favorite color &#187; Dojo Toolkit</title>
	<atom:link href="http://www.ghidinelli.com/c/webinternet/dojo-toolkit/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ghidinelli.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Jan 2017 17:45:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to bypass cross-domain restrictions when developing AJAX applications</title>
		<link>http://www.ghidinelli.com/2008/12/27/how-to-bypass-cross-domain-restrictions-when-developing-ajax-applications</link>
		<comments>http://www.ghidinelli.com/2008/12/27/how-to-bypass-cross-domain-restrictions-when-developing-ajax-applications#comments</comments>
		<pubDate>Sat, 27 Dec 2008 20:08:33 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Dojo Toolkit]]></category>
		<category><![CDATA[Research/HOWTO]]></category>
		<category><![CDATA[Web/Internet]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[ext]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[xmlhttprequest]]></category>

		<guid isPermaLink="false">http://www.ghidinelli.com/?p=461</guid>
		<description><![CDATA[I have been working with a contractor in India recently on a Javascript project using Ext.  I wanted to outsource the front-end development since we have extensive APIs that would take an outside developer longer to get up to speed on.  Due to PCI DSS and general security practices however, we can&#8217;t just [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working with a contractor in India recently on a Javascript project using Ext.  I wanted to outsource the front-end development since we have extensive APIs that would take an outside developer longer to get up to speed on.  Due to PCI DSS and general security practices however, we can&#8217;t just let the contractor log in and push code or make changes to our development server.  In fact, we didn&#8217;t even give him Subversion access meaning he would have to code against our data services remotely.</p>
<h2>The Setup</h2>
<p>Development server at dev.domain.com.  Off-site developer building an application on his laptop and connecting his AJAX/xmlHttpRequest calls to dev.domain.com.</p>
<h2>The Problem</h2>
<p>Browsers restrict cross-domain requests for security purposes.  So Javascript at http://localhost can&#8217;t retrieve data from a remote API like http://dev.domain.com.  In Firefox + Firebug, you would see the following:</p>
<p><code>Error: uncaught exception: Permission denied to call method XMLHttpRequest.open</code></p>
<p>There is a <a href="http://www.zachleat.com/web/2007/08/30/cross-domain-xhr-with-firefox/">hack for Firefox</a> but that doesn&#8217;t help you with IE, Safari or Chrome.   We started out this way but when it came time to deploy we found <a href="https://www.ghidinelli.com/2008/12/18/webkit-fail-on-javascript-indented-with-spaces">issues with Safari and Chrome</a>.  </p>
<p>These were things we could have detected much earlier in the development cycle had the Javascript developer been able to test in those browsers.  It was a big mistake on our part to delay the cross-browser testing and it&#8217;s a testament to the stability of the <a href="http://www.extjs.com">Ext components</a> that we didn&#8217;t have 10,000 other issues.</p>
<h2>The Solution</h2>
<p>If you&#8217;re developing with Apache, the answer is quite simple actually: use a reverse proxy.  Apache&#8217;s <a href="http://httpd.apache.org/docs/2.0/mod/mod_proxy.html">mod_proxy</a> will take a request for something like &#8220;/foo&#8221; and actually tunnel the request to some remote destination like &#8220;http://dev.domain.com/bar&#8221;.  The end result is that your web browser thinks you&#8217;ve made a call to http://localhost/foo but in reality you&#8217;re sending and retrieving data from a remote server.  Security implications solved!</p>
<p>I searched for a long time but never found an intersection between AJAX development, cross-domain xmlHttpRequest restrictions and Apache&#8217;s mod_proxy.  It wasn&#8217;t until I thought, &#8220;hey, that might work&#8221; and started searching specifically for reverse proxy details did I <a href="http://publib.boulder.ibm.com/infocenter/wsmashin/v1r0/index.jsp?topic=/com.ibm.websphere.sMash.doc/core/zero.core/docs/en/ProxyConfiguration.html">turn up an example</a>.</p>
<h2>Apache Configuration</h2>
<p>This is a pretty basic Apache feature &#8211; first you will need to load the required modules:</p>
<pre><code>LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so</code></pre>
<p>Let&#8217;s assume that I want to access a file at http://dev.domain.com/remote/api.php.  You would put all of the following into a &lt;VirtualHost&gt;:</p>
<pre><code># start mod_rewrite
RewriteEngine On

ProxyRequests Off
&lt;Proxy&gt;
	Order deny,allow
	Allow from all
&lt;/Proxy&gt;

ProxyPass /apitest/ http://dev.domain.com/remote/api/
ProxyPassReverse /apitest/ http://dev.domain.com/remote/api/
RewriteRule ^/apitest/(.*)$ /remote/api/$1 [R]</code></pre>
<p>Restart Apache and make a browser request to http://localhost/apitest/api.php and you should receive a response from the remote server at http://dev.domain.com/remote/api/api.php.  The RewriteRule will pass along any query-string parameters too.  Done!</p>
<p>If you use IIS instead of Apache, you should be able to do something similar with ISAPI_REWRITE to accomplish the same functionality.  This is also applicable to other technologies like <a href="http://www.adobe.com/products/flex/">Adobe Flex</a> or Flash which also have cross-domain restrictions.</p>
<p>The advantage over other solutions like a vanilla proxy server or server-side script that translates the request for you is that it requires few, if any, changes to your code.  It&#8217;s also payload-agnostic so it will work with any number of remote services without care for the request or response format.  </p>
<p>There&#8217;s one final tweak we could make so our development environment mirrors our staging and production servers.  If the final endpoint for the Javascript will be:</p>
<p><code>http://dev.domain.com/service/foo?var=value</code></p>
<p>Then we could use the same relative URL &#8220;/service/foo?var=value&#8221; and make our reverse proxy mirror that structure:</p>
<pre><code>ProxyPass /service/ http://dev.domain.com/service/
ProxyPassReverse /service/ http://dev.domain.com/service/ </code></pre>
<p>Now there is no change necessary in the Javascript; just make your request to &#8220;/service/foo?var=value&#8221; on either localhost or dev.domain.com and the browser will be properly routed to the right destination without running into any security restrictions!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghidinelli.com/2008/12/27/how-to-bypass-cross-domain-restrictions-when-developing-ajax-applications/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
