How to post XML to an iframe with JavaScript - Stack Overflow

Basically I want to post XML (with Content-Type textxml) to an URL and display the output directly in

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...

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...

Share Improve this question asked Sep 21, 2009 at 10:52 acmeacme 14.9k8 gold badges76 silver badges114 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

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./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 pat
idoc.open();
idoc.write('<form method="post" action="http://www.example./echoxml.php">');
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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745653662a4638414.html

相关推荐

  • How to post XML to an iframe with JavaScript - Stack Overflow

    Basically I want to post XML (with Content-Type textxml) to an URL and display the output directly in

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信