admin管理员组

文章数量:1292698

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:-

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

Any clue? Thanks.

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:-

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

Any clue? Thanks.

Share Improve this question asked Feb 1, 2011 at 16:33 limclimc 40.2k22 gold badges104 silver badges145 bronze badges 1
  • 1 Why don't you just use Firebug? – SLaks Commented Feb 1, 2011 at 16:40
Add a ment  | 

2 Answers 2

Reset to default 5

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

console.dirxml(xml) OR console.dir(xml) OR 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).

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?

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

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

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

or use .wrapAll()

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

本文标签: javascriptHow to display entire XML from JQuery39s AJAX callback functionStack Overflow