【发布时间】: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