Nitobi
About Nitobi
Services
Products
Home -> Blogs -> Dave Johnson

Dave Johnson

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 myObject.onXmlLoaded() and passes it the XML object that was created.

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:

if (typeof(eba_ajax_xmlp) == "undefined"){

    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(’This XML adata is from EnterpriseAjax.com‘, ‘1002′);
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, Architecture, JSON, XML. 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.

12 Responses to “Cross Domain AJAX with XML”

  1. Shogo Says:

    Does this work in Safari?

  2. Dave Johnson Says:

    Don’t think so - unless the latest version of Safari finally supports the DOMParser object.

  3. Tim Says:

    Any chance you could post a package with the full source code? Just wondering what the code looks like on the asp page.

  4. Dave Johnson Says:

    I will do so this weekend :)

  5. JasonKolb.com Says:

    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

  6. Dave Johnson Says:

    Ok maybe next weekend :S

  7. rayge Says:

    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?

  8. rimbaud Says:

    where’s the source code download?

    can u send me the source code?
    my Email:
    [email protected]

    thanks~!~!~!

  9. rp ny Says:

    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?

  10. Dave Johnson Says:

    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.

  11. lescoste.net » Blog Archive » Dave Johnson » Blog Archive » Cross Domain AJAX with XML Says:

    [...] Dave Johnson » Blog Archive » Cross Domain AJAX with XML Great tutorial ! [...]

  12. lescoste.net » Blog Archive » Dave Johnson � Blog Archive � Cross Domain AJAX with XML Says:

    [...] Dave Johnson � Blog Archive � Cross Domain AJAX with XML Great tutorial ! [...]

Leave a Reply


Search Posts

Pages

Archives

Categories

All contents are (c) Copyright 2006, Nitobi Software Inc. All rights Reserved
Dave Johnson Entries (RSS) and Comments (RSS).