Cross Domain AJAX with XML 
February 10th, 2006
On a post I made a few days back I proposed a way to do cross domain AJaX using XML rather than the commonly used JSON. It is essentially an extension of the idea of JSONP (JSON with Padding). Since I generally find myself working with XML more often than JSON I decided to create the equivalent for XML based applications. I have not extensively tested it but did try it on IE6 and FF1.5 on Win2K server.
So here it is. The idea is that we pass an id, a context and a method as querystring parameters to a server script such as http://www.enterpriseajax.com/cross_domain_xml.asp?context=myObject&method=onXmlLoaded&id=1000 and we get back some JavaScript. That JavaScript can then be injected as a script tag in your web page.
This will then return JavaScript code that creates an XML document object and loads an XML string into it. Once the XML string is loaded into the object it then calls a callback method such as
The id querystring parameter is used to uniquely identify each XML document that is requested, the conext is the object on which the callback is called and the method parameter is the name of the callback function.
The JavaScript returned from the pervious resource is this:
var eba_ajax_xmlp = {x: {}};
eba_ajax_xmlp.loadXml = function(s, uid){
if(document.implementation && document.implementation.createDocument) {
var objDOMParser = new DOMParser();
this.x[uid] = objDOMParser.parseFromString(s, “text/xml”);
} else if (window.ActiveXObject) {
this.x[uid] = new ActiveXObject(’MSXML2.DOMDocument.3.0′);
this.x[uid].async = false;
this.x[uid].loadXML(s);
}
}
}
eba_ajax_xmlp.loadXml(’
myObject.onXmlLoaded.call(myObject, eba_ajax_xmlp.x[’1002′]);
This is slightly different from the JSONP way of doing things but for the most part it’s the same sort of thing.
Try it out for your self with the sample page here - you should see an alert with contents of the loaded XML (just a root tag).
Note there is nothing on the Enterprise AJAX site but there will be soon
Del.icio.us
This entry was posted on Friday, February 10th, 2006 at 3:26 pm and is filed under AJAX, XML, Architecture, JSON. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

April 9th, 2006 at 9:26 pm
Does this work in Safari?
April 10th, 2006 at 10:52 am
Don’t think so - unless the latest version of Safari finally supports the DOMParser object.
April 25th, 2006 at 8:42 am
Any chance you could post a package with the full source code? Just wondering what the code looks like on the asp page.
April 27th, 2006 at 12:54 pm
I will do so this weekend
May 1st, 2006 at 10:23 am
Cross-domain SOAP from the browser
You know, writing about SOA vs. Web 2.0 earlier got me thinking. I think the real missing piece that needs to be addressed is a way to consume SOAP Web services, cross-domain, from the browser. That’ll make it possible to
May 3rd, 2006 at 2:37 am
Ok maybe next weekend :S
June 8th, 2006 at 4:01 pm
This seems to be a good workaround for the createRequestObject permissions error BUT. how could this be rewritten to handle checking for changes to the eba_ajax_xmlp.loadXml item every 5 seconds?
August 7th, 2006 at 11:02 pm
where’s the source code download?
can u send me the source code?
my Email:
[email protected]
thanks~!~!~!
August 12th, 2006 at 11:03 pm
seems like a fine idea. i know of a few cases where people have written a perl script to handle the http requests that go to another domain and return an xml ogject of the http resposnse to the js that called it. that isnt that different from what you are describing is it?
August 14th, 2006 at 2:03 pm
very similar except that the Perl script can be on someone elses server and you can just call it cross domain and it returns valid JS that creates an XML DOM document from an xml string.
February 9th, 2007 at 4:46 am
[…] Dave Johnson » Blog Archive » Cross Domain AJAX with XML Great tutorial ! […]
April 6th, 2007 at 9:50 am
[…] Dave Johnson � Blog Archive � Cross Domain AJAX with XML Great tutorial ! […]