Javascript Cross Domain Ajax

Since I joined Blast Radius I have been introduced to a lot of new and interesting development practices, frameworks and designs that I have always read or heard about, but was never really forced or motivated to imply in my daily progression. One such framework I wrote briefly about is Mojo. Being a framework that has evolved from the Dojo Toolkit my interest peaked this morning when I read a very interesting article on Ajaxian about cross domain transport.

Originally as more of a low level technologist who only picked up ajax and interface development as a smooth view state for communicating with my middle tier controllers I found Javascript to be very much lacking the web services department. Very often the techniques that have been traditionally used consist of one of two strategies for x-domain communication;

1- A remote javascript include is served using:
javascripthover edges to scroll
 
<script type='text/javascript href='http://remotehost/js/api.js?param=1&param2'>
This is usually a server side script that generates the file contents based on the parameters, and then either renders content locally into your DOM, or provides you with a set of methods that will allow you to manipulate the content. This is not a very secure model and does not allow you to interact with raw xml data very robustly, but as long as you trust the source that is rending the content, it is quite efficient.

2- By matter of Proxy.
This method I’ve used very often for direct communication with XML, SOAP, or REST gateways. Generally, I will develop the interface layer to communicate with a server side controller which will then use Curl to invoke the remote data, process and return as an JSON object. I find this method to be very robust, and allows you to create a very extensible gateway for your Javascript cross domain requests.

With that said, the post I read this morning is indicating that a new methodology for cross-domain scripting is emerging in the Dojo framework. By way of the property window.name one can make a remote request and retrieve data without exploiting the ever so wonderful cross domain security model that we are subject to in all our favorite browsers. This concept is very new but interesting. It opens up a whole new world of client side mash-up possibilities that are sure to get the creative juices flowing in the client side development space. Here is an example of it would be implemented:
javascripthover edges to scroll
 
dojox.io.windowName.send(method, args); // simple method
// deferred result
 
var deferred = dojox.io.windowName.send("GET", {url:"http://somesite.com/resource"});
deferred.addCallback(function(result){
  alert("The request returned " + result);
});

RSS feed | Trackback URI

2 Comments »

Comment by ari
2008-11-22 22:17:58

thanks for explaining the first method is a server side script. I thought as long as the script is on the other server, it can use xmlhttprequest on that server, but I’ve been getting a permission error and I’ve been spending the whole day trying to figure it out.
But now I see that it isn’t a javascript file on the other server, its a php (or other server side language) file that prints out javascript.

Comment by Kyle
2008-12-30 12:47:49

@ari – Yes this is correct. I almost always, relay through a server side proxy on my own server if I need my client side code to read anything from another server. I’m happy I could help you! Please feel free to send me a message if you have any more questions!

 
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post

23 queries in 0.221 seconds | Code is Poetry | sLajax.com