【问题标题】:Conversion from jQuery XML Object to String throws Security Error从 jQuery XML 对象到字符串的转换引发安全错误
【发布时间】:2012-03-17 02:04:52
【问题描述】:

我有一个由 jQuery 根据 REST Web 服务的响应生成的 XML 对象:

$.ajax({
    type: "GET",
    url: "http://localhost:9299/foo",
    success:function(xml) {
        xmlDoc = $.parseXML(xml);
        $xml = $(xmlDoc);
        // The rest of the code manipulates the structure of the XML
    }
});

现在我需要将更改后的 XML 对象输出为字符串。我已经为 Firefox 和其他浏览器找到了这个解决方案:

out = new XMLSerializer().serializeToString($xml);

但我在这里得到的只是以下错误消息:

[Exception... "Security error"  code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)"  location: "http://localhost/bar"]

我需要的所有文件都在本地主机上(包括为我提供 XML 和 jQuery 库的网络服务)

任何想法都将受到高度赞赏

编辑:

我已经简化了问题并尝试了以下代码:

$xml = $('<?xml version="1.0"?><root><element>bla</element><element>blubb</element></root>');
$xml.find("element").each(function() {
    alert($(this).text());
});
out = new XMLSerializer().serializeToString($xml);

即使没有任何网络服务调用,问题仍然存在。 (警报正确输出内容)

编辑 2:

感谢 Kevin B 的评论,我有了一个可行的解决方案:

$.ajax({
    type: "GET",
    url: "http://localhost:9299/foo",
    dataType: 'xml',
    success:function(xml) {
        var $xml = $(xml);
        // The rest of the code manipulates the structure of the XML
    }
});

最后一行没有改变:

out = new XMLSerializer().serializeToString($xml);

【问题讨论】:

  • 关于您的最新编辑。您没有将其解析为 XML。 jsfiddle.net/RKpua/7
  • 你的Edit2,如果你在我的回答中设置dataType: "xml",你不需要$.parseXML()

标签: javascript jquery xml dom


【解决方案1】:

首先,无论这是否是跨域请求,我都无法根据您的代码确认/拒绝。跨域是指外部文件的端口号、域或协议与请求外部文件的端口号、域或协议不同。

如果确实是跨域请求,则需要实现CORS或服务端代理为你请求。

其次,您不需要使用$.parseXML()。试试这个:

$.ajax({
    type: "GET",
    url: "/foo",
    dataType: "xml",
    success:function(xml) {
        var $xml = $(xml);
        // The rest of the code manipulates the structure of the XML
    }
});

XML 也必须有效,才能在所有浏览器中工作。

编辑:所以,这不是跨域问题,也不是 jquery 问题。这里还有一些调试:http://jsfiddle.net/RKpua/我在那里使用了一个非常简单的xml文档,你可以用你的xml替换简单的xml文档吗?

【讨论】:

  • 谢谢,包含 $.parseXML() 的行真的没有必要。不幸的是,它仍然不起作用。正如您所提到的,我的 Web 服务的端口号实际上与我使用 jquery 的界面不同。但这不重要,因为我将数据保存在局部变量 xml 中,然后才引用它,还是我错了?其余所需文件(如 jquery 库)在同一服务器(和端口)上可用。
  • 如果按照您的建议是跨域的,除非您实现 CORS 或使用服务器端代理脚本为您请求,否则无法使用 javascript 请求它。 en.wikipedia.org/wiki/Cross-origin_resource_sharing
  • 但是我已经能够加载 web 服务响应并更改/输出其 DOM 的不同部分(在相应的对象中)。为什么这行得通,而从本地对象到普通字符串的转换却不行?
  • 你可能已经实现了 CORS,如果你不这样做,它早就失败了。
  • 现在我已经简化了问题 - 没有成功。 (请参阅我帖子中的编辑)
【解决方案2】:

您不需要解析输出,因为 jQuery 会推断它。在任何情况下,您都可以指定数据类型。

$.ajax({
    type: "GET",
    url: "http://localhost:9299/foo",
    dataType: "xml",
    success:function(xml) {
        $xml = $(xmlDoc);
        // The rest of the code manipulates the structure of the XML
    }
});

【讨论】:

    【解决方案3】:

    您需要通过指定jquery对象中的第一个元素来访问jQuery对象的xml dom属性。

    out = new XMLSerializer().serializeToString($xml[0]);
    

    XMLSerializer 在 IE

    out = $xml[0].xml;
    

    或作为 jquery 扩展

    $.fn.xml2string = function(){
    if (window.XMLSerializer) {
        return (new XMLSerializer()).serializeToString(this[0]);
    } else if (typeof this[0].xml != "undefined") {
        return this[0].xml;
    }
    return "";
    };
    

    【讨论】:

    • 为什么 IE8 示例使用函数 xml() 而 xml2string 函数使用属性 xml。它不应该是两种情况下的财产吗? (或两种情况下的函数?)
    • 你是对的。 xml 是 IE8 中 XML 节点的一个属性。它不应该有括号。答案已更新。
    猜你喜欢
    • 2013-06-20
    • 2012-07-30
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多