views:

998

answers:

1

Basically I want to post XML (with Content-Type text/xml) to an URL and display the output directly in the browser. This has to be done in an iframe.

Is it possible to post the XML to an iframe? In other words, can the Content-Type of the Post-Request be changed to text/xml? PHP4 is also available if necessary.

The URL of the iframe-action has to remain because the Result contains a HTML page with relative links...

+1  A: 

It used to be possible to document.open(mimetype) on the iframe back in Netscape, but modern browsers don't support this (and Netscape never supported XML anyway).

In many browsers, you can set an iframe's src to a data URI such as: data:text/xml,%3Celement%3Ehello%3C/element%3E, as long as the document isn't too long. This doesn't work in IE though. So you would need at least a backup plan of going back to the server:

<?php
    header('Content-Type: text/xml');
    echo($_REQUEST('xml'));
?>

Then, if the XML were short enough to fit in a URI, you could set its src via:

iframe.src= 'http://www.example.com/echoxml.php?xml='+encodeURIComponent(xml);

If the XML might be long, you'd need to use a POST request, which means a form submission:

var idoc= iframe.contentDocument || iframe.contentWindow.document; // IE compat
idoc.open();
idoc.write('<form method="post" action="http://www.example.com/echoxml.php"&gt;');
idoc.write('    <textarea name="xml"></textarea>');
idoc.write('</form>');
idoc.close();
idoc.getElementsByTagName('textarea')[0].value= xml;
idoc.getElementsByTagName('form')[0].submit();

Is this all worth it? Unless you're using XSL, the unstyled XML view you'd get in most browsers would probably be quite poor. In older/simpler browsers that don't display XML at all you'd just be prompted to download the XML file.

And it's dangerous to allow anyone to inject any XML content into your security context. eg. if they made a return-document containing XHTML scripting content, you'd be vulnerable to cross-site-scripting attacks.

bobince
Thanks! The problem is, I need to send Login-Data to a Webservice that returns plain HTML or a 302-redirect to a HTML-page. And the credentials may only be passed with a POST-Request and Content-Type XML. And I don't know how to achieve this. I tried CURL which works quite well - the only downside: the returned HTML contains Image- and Stylesheet-Links with relative data. And because CURL does still hold the original script-path in the location bar the relative paths don't work :-(
acme
Ah, OK, you want to send an XML request body, that's a bit of a different thing. Sounds a bit weird that it returns HTML with relative paths when a browser could never send the request... you would have to post-process the returned HTML to fix up the addresses or add a `<base>` element after using curl, which is a bit of a hack. If the web service is local you could also use an XMLHttpRequest on the client side to sent the XML body.
bobince
I now "solved" it by parsing the returned html from curl and adding the base-tag right after the head-tag. It's pretty ugly but at least it works - and obviously seems to be the only way to do it. Thank you again!
acme