2012-02-27 54 views
3

我从REST web服务的响应的jQuery生成的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"] 

所有的文件,我需要的是在本地主机上(包括web服务这为我提供

任何想法,将不胜感激的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); 

即使没有任何Web服务调用的问题依旧。 (警报内容正确输出)

编辑2:

感谢凯文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); 
+1

关于您最新的编辑。你没有把它解析为XML。 http://jsfiddle.net/RKpua/7/ – 2012-02-27 17:19:40

+1

你的Edit2,如果你在我的答案中设置'dataType:“xml”',你不需要'$ .parseXML()' – 2012-12-27 16:21:04

回答

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文档吗?

+0

谢谢,包含$ .parseXML()实际上是不必要的。不幸的是,它仍然无法正常工作。正如你所提到的,我的Webservice的端口号实际上与我的jQuery使用界面不同。但那应该不重要,因为我将数据保存在局部变量xml中,并且只在之后引用它,或者我错了吗?其余的必需文件(如jquery库)可在同一台服务器(和端口)上使用。 – Velarion 2012-02-27 16:34:54

+0

如果它是跨域的,建议您不要使用javascript请求它,除非您使用CORS或使用服务器端代理脚本来为您请求它。 http://en.wikipedia.org/wiki/Cross-origin_resource_sharing – 2012-02-27 16:38:46

+0

但我已经能够加载web服务响应和改变/输出其DOM(在相应的对象)的不同部分。为什么这个工作正常,并且从本地对象到普通字符串的转换不是? – Velarion 2012-02-27 16:43:43

0

您不需要解析输出,因为jQuery推断它。无论如何你可以指定dataType。

$.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 
    } 
}); 
0

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

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

另外XMLSerializer在IE <中不可用9。对于IE8使用以下

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 ""; 
}; 
+0

为什么IE8示例使用函数'xml()'而xml2string函数使用属性'xml'。它不应该是两种情况下的财产吗? (或在两种情况下的功能?) – 2015-01-29 10:27:26

+0

你是对的。 xml是IE8中XML节点的属性。它不应该有括号。答案已更新。 – Matt 2015-03-12 00:56:32