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

时间:2022-08-19 19:35:49

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

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

$.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.

任何线索?谢谢。

2 个解决方案

#1


5  

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 prettyPrint to 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)?

#2


1  

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());

#1


5  

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 prettyPrint to 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)?

#2


1  

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());