While working on the product for Intense Debate I recently ran into a little snafu. For those of you that don’t know this (which I didn’t until just recently), Ajax does not work across domains. In other words, a page on example.com can not send Ajax requests to another page at anothersite.com. Apparently this was done as a security precaution…which I don’t really understand since just about every other method of communication on the web works fine from one domain to another. It was definitely a set back for our project though….we rely quite heavily on Ajax (between domains) for our product to work.
Turns out there is a relatively simple (though not quite as nice) work around with a creative use of the script tag. Essentially, you use a newly created script tag that passes values inline in the url back to the remote server. The server then returns a script that calls whatever function would normally be called on a successful post of the traditional Ajax methods. It’s not quite as clean, and some browsers will virtually hang during the communication stage, but it allows dynamic background communication. In other words, it works.
This was definitely a potentially large setback. It would’ve all but meant the death of our product had we not found a functional workaround. And thus I wanted to write this post as a resource for those who might not already know this. Ajax does not work across domains. And the work around is a creative use of the script tag. Feel free to email me if you happen to need a little more in depth explanation of this technique. It really is rather simple though. Just wish someone would’ve told me…could’ve saved me a couple of hours of confusion and research as to why my application didn’t work.