javascript 如何在 PRE 标记中显示来自 AJAX 请求的 XML 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8522073/
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-26 03:42:13  来源:igfitidea点击:

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

javascriptjqueryxmlajax

提问by Richard Knop

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

我正在使用 jquery 向使用 XML 响应的 Web 服务发出 AJAX 请求:

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

我想在 PRE 标记内的 HTML 页面中显示来自 Web 服务的 XML 响应。但是上面的代码不起作用。如何将 XML 响应更改为字符串并将其显示在 PRE 标记中?

回答by ShankarSangoli

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>');
    }
});

回答by Alex Figueiredo

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

});

回答by LA SN

$.ajax({
type: "GET",
url: "/static/css/xml/xmlname.xml",
dataType: "xml",
success: function(xml){
     //now you can use of your response like
$("your tagName",$(this)).each(function(){
     alert($(this).attr('class'))

})

}
})

Note:the XML response can be access only in success function

注意:XML 响应只能在成功函数中访问

回答by Ricardo Souza

You can pass the data though an HTML encode function like: http://www.strictly-software.com/htmlencode

您可以通过 HTML 编码功能传递数据,例如:http: //www.strictly-software.com/htmlencode

$("#preForXMLResponse").html(HTMLEncode(xmlResponse.xml));

Note that HTMLEncodeis a pseudo function just to clarify my answer and must be changed to a function name like the one from the link above, aside with its implementation.

请注意,这HTMLEncode是一个伪函数,只是为了澄清我的答案,除了它的实现之外,还必须将其更改为类似于上面链接中的函数名称。