July 22nd, 2008 - Cross Domain Ajax

I’ve recently been playing with cross domain ajax and trying to figure out a way to do it without polling the hash in the location.

I first played around with the trick that Abe Fettig came up with (http://www.fettig.net/weblog/2005/11/30/xmlhttprequest-subdomain-update/) but it had one problem.  It required the page to set the document.domain.  I was looking to using this in a very large code base that can not have document.domain set as it is close to impossible to find all the places that will require it.

I then played around with the xip.js file that is in dojo, but didn’t like it as it used polling and I don’t really want to use polling.

So I worked with those two ideas to see if I could get a working/non-polling solution.  I was able to successfully change Abe Fettig’s idea to work as a inner iframe of a parent page that doesn’t change its document.domain.  This worked correctly in FF2, IE6, and IE7.  Safari didn’t like it though.

I then found this bug request to dojo: http://trac.dojotoolkit.org/ticket/4306.  That got me looking at various events that I could fire.  The blur() and focus() didn’t work as it caused the focus to be lost.  But then I discovered onresize.  I could resize the iframe, which would call the onresize event inside the iframe.  And it worked perfectly in Safari (but not in IE).

So I’ve taken those two ideas, combined it with the window.postMessage event, and have created a plugin to Ajax.Request inside of prototype.  This only works for subdomain ajax requests because of the document.domain but that is what I needed.

Oh, and as for why I couldn’t just use a server side proxy, I was trying to get around the 2 requests per domain restriction.

2 Responses to “Cross Domain Ajax”

  1. JDD Says:

    Would you please post the source of your plugin ?

  2. admin Says:

    I can’t post the source as it was done for my employer and so is not open sourced.

Leave a Reply