问题描述
我有一个由REST Webservice的响应由jQuery生成的XML对象:
$。ajax({
type:GET,
url:http:// localhost:9299 / foo,
success:function(xml){
xmlDoc = $ .parseXML(xml)
$ xml = $(xmlDoc);
//其余的代码操作XML
}
}的结构);
现在我需要输出改变的XML对象作为字符串。我已经为Firefox和其他浏览器找到了这个解决方案:
out = new XMLSerializer()。serializeToString($ xml);
但是我到这里收到以下错误消息:
[异常...安全错误代码:1000nsresult:0x805303e8(NS_ERROR_DOM_SECURITY_ERR)位置:http:// localhost / bar]
我需要的所有文件都在本地主机上(包括提供XML和jQuery库的webservice) p>
任何想法都将被高度赞赏
编辑:
我已经简化了问题,并尝试了以下代码:
$ 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);
//其余代码操纵XML
}
}的结构);
最后一行不会更改:
out = new XMLSerializer()。serializeToString($ xml);
首先,我无法确认/拒绝对您的代码,无论这是否是跨域请求。跨域是当外部文件的端口号,域或协议与请求外部文件的端口号,域或协议不同时。
如果确实是跨域请求,您需要实施或服务器端代理,为您请求。
其次,您不需要使用 $。parseXML()
。尝试这样:
$。ajax({
type:GET,
url: foo,
dataType:xml,
success:function(xml){
var $ xml = $(xml);
//其余的代码操作XML
}
}的结构);
XML也必须有效,可以在所有浏览器中工作。
编辑:所以,这不是一个跨域问题,它不是一个jquery问题。这里有一些更多的调试:我在这里使用了一个非常简单的xml文档,您可以用xml替换简单的xml文档吗?
I have a XML Object generated by jQuery from the response of a REST Webservice:
$.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
}
});
Now I need to output the altered XML Object as a String. I've already found this solution for Firefox and other browsers:
out = new XMLSerializer().serializeToString($xml);
But all I get here is the following error message:
[Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)" location: "http://localhost/bar"]
All files I need are on localhost (including the webservice which provides me the XML and the jQuery library)
Any ideas would be highly appreciated
Edit:
I've simplified the problem and tried the following code:
$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);
Even without any webservice call the problem remains the same. (The alert outputs the content correctly)
Edit 2:
Thanks to the comment by Kevin B, I've got a working solution:
$.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
}
});
The last line doesn't change:
out = new XMLSerializer().serializeToString($xml);
First off, I can't confirm/deny based on your code whether or not this is a cross-domain request. Cross-domain is when the port number, domain, or protocol of the external file is different from the one requesting the external file.
If it is indeed a cross-domain request, you need to implement CORS or a server-side proxy to request it for you.
Secondly, you don't need to use $.parseXML()
. Try this:
$.ajax({
type: "GET",
url: "/foo",
dataType: "xml",
success:function(xml) {
var $xml = $(xml);
// The rest of the code manipulates the structure of the XML
}
});
The XML also must be valid for it to work in all browsers.
Edit: So, it's not a cross-domain issue, and it is not a jquery issue. Here's some more debugging: http://jsfiddle.net/RKpua/ I used a very simple xml document there, can you replace the simple xml document with your xml?
这篇关于从jQuery XML对象到String的转换会抛出安全错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!