If you've tried using AJAX via your browsers XMLHttpRequest object to access data from another domain, you are greeted with an error similar to XMLHttpRequest cannot load – Origin is not allowed by Access-Control-Allow-Origin.

In a nutshell, this means the JavaScript is restricting your AJAX calls to within your domain, or at least that's what the docs say. What they often don't say is that this same rule applies across subdomains within your domain.

This is of particular annoyance if you are trying to develop a mobile version of your website, which leverages parts of your primary site. The gurus out there tell you to use JSONP, which works just fine.

But, if you're like me, you've grown particular fond of the concise little jQuery $.ajax() function, and hate to give it up!

Fortunately, if your subdomains are on the same server, you can create a symbolic link (Linux) to the directory, or files you need to access within your primary site, i.e.

ln -s /path/to/my/primary/site/functions functions

Now, from your mobile site, you can access the functions from your primary site, simply by referencing the symbolic link. The browser has no idea what's going on server-side, so you're back in the AJAX game!


Leave a Reply

Your email address will not be published. Required fields are marked *