2012-09-17 18 views
0

A有以下問題。我的代碼發送HTTP請求到ASP處理程序:null Chrome中的responseXML.xml值

var xmlRequest = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') :  new XMLHttpRequest; 
xmlRequest.open("GET", "Handler.ashx?value25="+$('#joKod').val()+"&operType=3"); 
xmlRequest.setRequestHeader('Content-Type', 'text/xml'); 
xmlRequest.onreadystatechange = function() 
{ 
    if (xmlRequest.readyState == 4) 
    { 
     if (xmlRequest.status == 200) 
     { 
      alert(xmlRequest.responseXML.xml);  
     } 
    } 
} 
xmlRequest.setRequestHeader("Content-Type", "text/xml"); 
xmlRequest.send('xml'); 

我的處理程序創建XML答案

XmlDocument xd=Scoring.CheckLinkFirma(okpo); 
resp.Clear(); 
resp.ContentType = "text/xml"; 
xd.Save(resp.Output); 

並將其發送回:

<?xml version="1.0" encoding="utf-8"?> 
<Root> 
    <jo status="client">abc</jo> 
    <jo status="client">dfh</jo> 
</Root> 

在Exlorer responseXML.xml具有價值,但Chrome和FF是未定義的?

回答

0

獲得XML文本我發現jQuery的答案。希望它可以幫助某人

$.ajax 
({  
    type: "GET", 
    url: "Handler.ashx?value25="+$('#joKod').val()+"&operType=3", 
    dataType: "text", 
    cache: true,    
    success: function(xml) 
    {    
     if (xml) 
     { 
      var myXml = $($.parseXML(xml)).find('Root');  
     } 
    } 
}); 
1

因爲那是IE唯一的屬性。如果你想獲得一個XML,你可以做這樣的事情:

function XMLToString(oXML) 
{ 
if (window.XMLHttpRequest) {}//Standards First 
else if (window.ActiveXObject) {return oXML.xml;}//Proprietary Second 
else {return (new XMLSerializer()).serializeToString(oXML);}//other browsers 
} 
alert(XMLToString(xmlRequest.responseXML)) 

或者你可以嘗試從xmlRequest.responseText

+0

謝謝,XMLToString使xmlRequest.responseXML屬性已經是XML,並且不需要再次使它xml。 Chrome調試顯示xmlRequest.responseXML是對象並且包含標籤。但他們都是不明確的。並且任何節點的nodeName字段是#text – Alex

+0

IE中的responseXML.xml返回XML文檔的** text **,而不是對象。但.xml是XMLDocument對象的IE特有屬性,因此如果您想採用xml文檔crossbrowser的文本 - 您可以從我的答案中使用XMLToString函數。它也接受XML節點。 –

+0

+1這是我需要的。可能應該被接受爲答案。 – JDB