javascript 如何从 JQuery 的 AJAX 回调函数显示整个 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4865220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:09:05  来源:igfitidea点击:

How to display entire XML from JQuery's AJAX callback function?

javascriptjquery

提问by limc

I have this simple code to query against the web service:-

我有这个简单的代码来查询网络服务:-

$.get( url ,  
    function(xml) {
        var hello = $(xml).find("hello").text();
        ...

        alert(xml); // displays [object XMLDocument]
        alert($(xml)); // displays [object Object]
    }                       
);

This works fine, but I'm interested to see entire XML structure from the callback function for debugging purpose. I tried a few things, but I couldn't get it to display the XML. What I want to see is something like this:-

这工作正常,但我有兴趣从回调函数中查看整个 XML 结构以进行调试。我尝试了一些东西,但我无法让它显示 XML。我想看到的是这样的:-

<stuff>
    <hello>bear</hello>
</stuff>

Any clue? Thanks.

有什么线索吗?谢谢。

回答by Scoobler

If you are using firebug in firefox, but may also work in IE8 or chrome, you can try:

如果您在 firefox 中使用 firebug,但也可能在 IE8 或 chrome 中工作,您可以尝试:

console.dirxml(xml)OR console.dir(xml)OR console.log(xml)

console.dirxml(xml)console.dir(xml)console.log(xml)

In IE8 press F12 to open up the developers console or you may get the error saying the normal browser doesn't know what console is (after F12 it should).

在 IE8 中按 F12 打开开发者控制台,否则你可能会收到错误消息,说普通浏览器不知道控制台是什么(在 F12 之后应该)。

Alternatively you could use prettyPrintto display the object. Or take a look at the answer to Is there an equivalent for var_dump (PHP) in Javascript?

或者,您可以使用PrettyPrint来显示对象。或者看看在 JavaScript 中是否有 var_dump (PHP) 的等价物的答案

回答by jAndy

You would need a little trickery. Introduce a new element, which wraps your XML and output the .html()from that. Like

你需要一点技巧。引入一个新元素,它包装您的 XML 并.html()从中输出。喜欢

var fakexml = "<stuff><hello>bear</hello></stuff>";

alert($('<debug>').append(fakexml).html());

or use .wrapAll()

或使用 .wrapAll()

alert($(fakexml).wrapAll('<debug>').parent().html());