渲染XML文件(通过Ajax调用获得),以一个新的窗口 [英] Render XML document (obtained through ajax call) to a new window

查看:117
本文介绍了渲染XML文件(通过Ajax调用获得),以一个新的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在寻找一种方式来呈现一个XML文档,我检索使用AJAX,一个新的浏览器窗口。

Hi I'm looking for a way to render an XML document, that I retrieve using ajax, to a new browser window.

我使用jQuery的阿贾克斯()函数张贴JSON数据到一个MVC控制器。控制器返回XML作为一个字符串。

I am using JQuery's ajax() function to post JSON data to an MVC controller. The controller returns XML as a string.

我使用的window.open()使用JavaScript创建一个新的窗口,并通过调用设置内容的文件。

I am using window.open() to create a new window in javascript and set the documents content by calling.

newwindow.document.clear();
newwindow.document.            
newwindow.document.write(jqXHR.responseText);
newwindow.document.close();

(凡jqXHR.responseText从阿贾克斯()调用返回的XML。)

(Where jqXHR.responseText is the XML returned from the ajax() call.)

在新窗口中打开预期,如果我查看源代码的网页上,我看到我的XML。但(你知道一人来了)没有出现在浏览器窗口。显然,如果我保存网页的源文件到磁盘,打开输出呈现为预期。

The new window opens as expected and if I view source on the page I see my XML. BUT (you knew one was coming) nothing appears in the browser window. Obviously if I save the page source to disk and open the output is rendered as expected.

任何人都可以提出一个解决方案吗? 只是为了再次重申我的主要目标是呈现一个XML文档(通过Ajax调用获得),以一个新的窗口。

Can anyone suggest a solution? Just to re-iterate my main goal is to render an XML document (obtained through ajax call) to a new window.

我还要补充一点,我想看到​​一个XSLT转换输出。我的XML有此处理指令。 非常感谢

I should also add that I would like to see the output transformed by an XSLT. My XML has this processing instruction. Many Thanks

修改 ---------------------------我去的解决方案------- ------------------

Edit--------------------------- THE SOLUTION I WENT WITH -------------------------

感谢大家的意见和建议。

Thanks for everyone's comments and suggestions.

这是我结束了去解决的办法是有目标的一种形式=_空白然后,我写了JSON的形式隐藏字段,并将其贴到返回的XML我的控制器(从JSON构建)。当XML从响应中返回的浏览器标记它如预期。我想这不是一个答案,原来的问题。但爱说话的下面有个解决方案。

The solution that I ended up going with was to have a form with target="_blank" I then wrote the JSON to the form as a hidden field, and posted it to my controller which returned the XML (constructed from the JSON). When the XML was returned from the response the browser marked it up as expected. I guess this is not an answer to the original question. But Gabby has a solution below.

推荐答案

下面将只在 火狐歌剧,但我认为这是值得一提的。

The following will work only in FireFox and Opera, but i think is worth mentioning ..

window.open('data:text/xml,' + encodeURIComponent( jqXHR.responseText ) );

应与镀铬的工作很好,但它似乎把的window.open 不同于通常的网址..如果你只需要输入所产生的URL中铬它的工作原理那里以及..

should work with chrome as well but it seems to treat window.open differently than a usual URL.. if you just type the resulting url in chrome it works there as well..

更新 这适用于所有的浏览器!

的事情是,JavaScript有使用XSLT变换分析XML的能力。
但不是自动的,所以我们需要找到引用了XSLT文件的XML文件并加载为好。然后,我们可以做的JavaScript中的转化和生成的HTML传递给新的窗口。

The thing is that javascript has the ability to tranform xml using xslt.
But not automatically, so we need to find the XML file for the reference to the XSLT file and load that as well. Then we can do the transformation in javascript and pass the resulting html to the new window.

当然IE处理事情的方式不同于其他地区。

Naturally IE handles thing differently than the rest.

$.get('xml-file-here.xml',
   function(xmlData){
                  var xml = xmlData;

                  //extract the stylesheet so we can load it manually
                  var stylesheet;
                   for (var i=0;i<xml.childNodes.length;i++){
                       if ( xml.childNodes[i].nodeName =='xml-stylesheet' )
                       {
                        stylesheet = xml.childNodes[i].data;
                       }
                   }
                  var items = stylesheet.split('=');
                  var xsltFile = items[items.length-1].replace(/"/g,'');

                  //fetch xslt manually
                  $.get( xsltFile, function(xsltData){
                      var xslt = xsltData;
                      var transformed;

                      if (! window['XSLTProcessor'])
                        {
                            // Trasformation for IE
                            transformed = xml.transformNode(xslt);
                        }
                        else
                        {
                            // Transformation for non-IE
                            var processor = new XSLTProcessor();
                            processor.importStylesheet(xslt);
                            var xmldom = processor.transformToDocument(xml);
                            var serializer = new XMLSerializer();
                            var transformed = serializer.serializeToString(xmldom.documentElement);
                        }

                      var newwindow = window.open();
                      newwindow.document.open();
                      newwindow.document.write(transformed);
                      newwindow.document.close();
                  });
   });

这篇关于渲染XML文件(通过Ajax调用获得),以一个新的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆