Search code examples
jqueryxmljsonjsonp

Is there a way to get JSONP-like fetching of XML in jQuery?


For a web service I'm developing I would like my embedded code (on the client's site) to fetch an XML file from my sever script which resides on my domain.

As this is a cross-domain request I figured to use JSONP as it seems the de facto standard for such APIs. However, for my application it would be easier for me to use XML instead of JSON. Now, I could of course convert my XML to JSON on the server and then back again to XML in the client's site JavaScript, but that seems unnecessarily cumbersome. What I really need is and XMLP solution, XML with padding.

I tired googling but couldn't find a solution that does that. Does anyone know a simple solution?


Solution

  • The only reason json works is because included javascript on your page can run in the window without any x-domain issues. So it must remain javascript. However, you could just minify the xml, make sure it's properly escaped and send it as a value in a json object.

    echo 'callback({data: "' + xml string + '"});';
    

    Or something along those lines.