07/23/08
Javascript Cross Domain Ajax
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¶m2'>
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);
});
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.
@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!