如何从 jQuery 返回的 XMLDocument 中获取整个 XML 字符串(跨浏览器)?
我尝试过,但未能找到如何从 GET 返回的 XMLDocument 中获取整个 XML 字符串。关于如何查找或替换对象中的特定元素,有很多问题,但我似乎找不到任何关于如何将整个文档作为字符串获取的答案。
我正在使用的示例来自此处。 “用 xml 做一些事情”部分就是我现在所处的位置。我感觉这应该是非常微不足道的,但我不知道如何做到。是否有“xml.data()”或类似的可用于此目的?
$.ajax({
url: 'document.xml',
type: 'GET',
dataType: 'xml',
timeout: 1000,
error: function(){
alert('Error loading XML document');
},
success: function(xml){
// do something with xml
}
});
用例是我想将 xml 提供给 flash 插件,为此我需要实际的 XML 作为字符串。
I have tried and failed to find out how to get the entire XML string from the XMLDocument returned by a GET. There are a lot of questions on SO on how to find or replace specific elements in the object, but I can't seem to find any answer to how to get the entire document as a string.
The example I'm working with is from here. The "do something with xml"-part is where I'm at at the moment. I get the feeling that this should be really trivial, but I fail to find out how. Is there an "xml.data()" or similar that can be used for this purpose?
$.ajax({
url: 'document.xml',
type: 'GET',
dataType: 'xml',
timeout: 1000,
error: function(){
alert('Error loading XML document');
},
success: function(xml){
// do something with xml
}
});
The use case is that I want to feed the xml to flash plugin and for that I need the actual XML as a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果两者都需要,请获取 XML 文档和字符串形式的响应。您应该能够执行
如果您希望将其作为字符串,为什么您指定
dataType:xml
那么dataType:text
不会更合适?If you want both, get the response as XML Document and as string. You should be able to do
If you want it as string why do you specify
dataType:xml
wouldn't thendataType:text
be more appropriate?您希望它作为纯文本而不是 XML 对象吗?将
dataType
从'xml'
更改为'text'
。有关更多选项,请参阅 $.ajax 文档。You want it as plain text instead of XML object? Change
dataType
from'xml'
to'text'
. See the $.ajax documentation for more options.您还可以在 Java 脚本中轻松地将 xml 对象转换为字符串:
You can also easily convert an xml object to a string, in your java script:
如果您只需要一个表示从 jquery 返回的 xml 的字符串,只需将数据类型设置为“text”,而不是尝试将 xml 解析回文本。以下内容应该只是从 ajax 调用中返回原始文本:
If you only need a string representing the xml returned from jquery, just set your datatype to "text" rather than trying to parse the xml back into text. The following should just give you raw text back from your ajax call:
虽然这个问题已经得到解答,但我想指出一个警告:当通过 Internet Explorer 使用 jQuery 检索 XML 时,您必须将内容类型指定为“text/xml”(或“application/ xml"),否则您将无法使用 jQuery 解析数据,就好像它是 XML 一样。
你可能认为这是一件显而易见的事情,但当我使用 Mozilla/Chrome/Opera 而不是 IE 时,它引起了我的注意。当检索内容类型为“文本”的 XML“字符串”时,除 IE 之外的所有浏览器仍然允许您解析该数据(使用 jQuery 选择器),就好像它是 XML 一样。 IE 不会抛出错误,也不会向 jQuery 选择语句返回任何结果。
因此,在您的示例中,只要您只需要 XML 的字符串序列化版本,并且不希望 jQuery 在 XML DOM 上进行任何类型的选择,您就可以将 content-type 设置为“text”。但是,如果您还需要使用 jQuery 解析 XML,则需要编写一个自定义例程,将 XML 序列化为字符串,或者检索内容类型为“xml”的 XML 版本。
希望对某人有帮助:)
Although this question has already been answered, I wanted to point out a caveat: When retrieving XML using jQuery with Internet Explorer, you MUST specify content-type to be "text/xml" (or "application/xml") or else you will not be able to parse the data as if it were XML using jQuery.
You may be thinking that this is an obvious thing but it caught me when using Mozilla/Chrome/Opera instead of IE. When retrieving a "string" of XML with a content-type of "text", all browsers except IE will still allow you to parse that data (using jQuery selectors) as if it were XML. IE will not throw an error and will simply not return any results to a jQuery selection statement.
So, in your example, as long as you only need the string-serialized version of the XML and will not expect jQuery to do any sort of selection on the XML DOM, you can set the content-type to "text". But if you ALSO need to parse the XML with jQuery, you will need to write a custom routine that serializes the XML into a string for you, or else retrieve a version of the XML with content-type "xml".
Hope that helps someone :)
您可以获得请求中使用的本机 XMLHttpRequest 对象。
在我发布这个答案时,jQuery 文档说明了几种方法。
其中之一是通过成功回调的第三个参数:
对于完整的示例:
https://jsfiddle.net/44m09r2z/
You can get the native XMLHttpRequest object used in the request.
At the time i'm posting this answer, jQuery docs state a few ways to do so.
One of them is via the third argument of the success callback:
For a complete example:
https://jsfiddle.net/44m09r2z/