how to display XML response from AJAX request in a PRE tag

13,708

Solution 1

Try this

$.ajax({
    type: "GET",
    url: $uri,
    dataType: "xml",
    async: false,
    contentType: "text/xml; charset=\"utf-8\"",
    success: function(xmlResponse) {
        $("#preForXMLResponse").html('<pre>'+xmlResponse+'</pre>');
    }
});

Solution 2

Try this:

$(function(){

    $.ajax({
         type: "GET",
         url: $uri,
         dataType: "xml",
         async: false,
         contentType: "text/xml; charset=\"utf-8\"",
         complete: function(xmlResponse) {

                // So you can see what was wrong...
                console.log(xmlResponse);
                console.log(xmlResponse.responseText);

              $("#preForXMLResponse").text(xmlResponse.responseText);
         }
    });

});
Share:
13,708
Richard Knop
Author by

Richard Knop

I'm a software engineer mostly working on backend from 2011. I have used various languages but has been mostly been writing Go code since 2014. In addition, I have been involved in lot of infra work and have experience with various public cloud platforms, Kubernetes, Terraform etc. For databases I have used lot of Postgres and MySQL but also Redis and other key value or document databases. Check some of my open source projects: https://github.com/RichardKnop/machinery https://github.com/RichardKnop/go-oauth2-server https://github.com/RichardKnop

Updated on June 15, 2022

Comments

  • Richard Knop
    Richard Knop almost 2 years

    I am using jquery to make an AJAX request to a web service which responds with XML:

    $.ajax({
        type: "GET",
        url: $uri,
        dataType: "xml",
        async: false,
        contentType: "text/xml; charset=\"utf-8\"",
        complete: function(xmlResponse) {
            $("#preForXMLResponse").html(xmlResponse);
        }
    });
    

    I want to display the XML response from the web service in a HTML page inside PRE tag. But the code above does not work. How can I change the XML response to a string and display it inside PRE tag?

  • Richard Knop
    Richard Knop over 12 years
    xmlResponse is [object Object]. So all I get in the pre tag is [object Object]. I want the XML as string there.
  • Ricardo Souza
    Ricardo Souza over 12 years
    Use the xml property xmlResponse.xml
  • Richard Knop
    Richard Knop over 12 years
    xmlResponse is not a string, so it will not work. Web service outputs true XML with applciation/xml mime type.