2011-12-15 71 views
2

我使用jQuery作出AJAX请求与XML响应Web服务:如何显示来自AJAX请求在PRE标签XML响应

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

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

回答

3

试试这个

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

xmlResponse是[对象的对象。所以我得到的pre标签是[object Object]。我希望将XML作为字符串存在。 – 2011-12-15 15:12:09

0

可以传递数据虽然一个HTML编码功能,如:http://www.strictly-software.com/htmlencode

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

注意HTMLEncode是一个伪功能只是为了澄清我的答案,必须改为一个函数名称就像上面的链接一样,除了它的实现。

3

尝试这种情况:

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

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

}) 

} 
}) 

注:XML响应可以访问仅在成功函数