我有一个XML对象,是由jQuery从REST Web服务的响应中生成的:
现在我需要将修改后的XML对象输出为字符串。我已经为Firefox和其他浏览器找到了此解决方案:
但我在这里遇到的问题是以下错误信息:
最后一行不会改变:
$.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的Web服务和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);
即使没有任何webservice调用,问题仍然存在。(警告正确输出内容)
编辑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);
dataType: "xml"
, 您就不需要使用$.parseXML()
。 - Kevin B