0

how do i display xml data using jquery? i don't need to parse it i just need to display it to the user on the page.

Here's my current code:

$.ajax({
    type: "POST",
    dataType: "xml",
    contentType: "application/json; charset=utf-8",
    url: "Service2.svc/DoWork",
    data: "{}",
    processdata: true,
    success: function(response, textStatus, jqXHR) {
        alert($(jqXHR).responseXML);
    }); $("#Text1").val($(response));
}, error: function() {
    alert("error");
}
});
Blender
  • 289,723
  • 53
  • 439
  • 496
user384080
  • 4,576
  • 15
  • 64
  • 96

4 Answers4

1

I believe this will work:

$.ajax({
    success: function(response, textStatus, jqXHR) {
        alert(jqXHR.xml);
    }
});
Eli
  • 17,397
  • 4
  • 36
  • 49
0

I think you'll need to parse it anyways due to the < and > . Use the 'load' method.

Vicente Plata
  • 3,370
  • 1
  • 19
  • 26
  • okay.. how bout if I display the xml in alert method? i've tried alert($(xml)); but it just displays "object [object]". any ideas? – user384080 Apr 23 '11 at 05:19
  • Please let us see your code. Why are you using the '$' char before the variable? JavaScript is not Perl or PHP ;) . – Vicente Plata Apr 23 '11 at 05:21
  • Because the jQuery function `$()` converts a HTML string (hopefully XML) into a jQuery object when passed as a first parameter. Like so: `$('
    asd
    ');`
    – Blender Apr 23 '11 at 05:28
  • `$.ajax({ type: "POST", dataType: "xml", contentType: "application/json; charset=utf-8", url: "Service2.svc/DoWork", data: "{}", processdata: true, //True or False success: function (response, textStatus, jqXHR) { alert($(jqXHR).responseXML); }); $("#Text1").val($(response)); }, error: function () { alert("error"); } });` – user384080 Apr 23 '11 at 05:30
0

I'm assuming you've stored the response XML into the variable xml.

I think this should work:

alert($('<div>').append($(xml).clone()).remove().html());

Credit goes here: How do you convert a jQuery object into a string?

Community
  • 1
  • 1
Blender
  • 289,723
  • 53
  • 439
  • 496
0

You can just use result.responseText where result is the object returned in your complete function

Namwar Rizvi
  • 143
  • 3