2009-08-16 60 views
0

我真的很感激得到你的幫助,因爲我似乎無法檢測和解決我在使用我正在開發的網站上的AJAX功能所遇到的問題。AJAX XML回覆節點值迭代

我有一個webform,它對處理程序(.ashx)進行異步調用,該處理程序提供XML響應,稍後由將其內容放入用戶界面的Javascript客戶端函數進行處理。

我附上我的處理程序生成的響應的一個例子,我想知道什麼是我能得到的所有<body>元素innerHTML(與文本和子節點)的內容將其追加到<span>元素在用戶界面上。任何人都可以幫我解決這個問題嗎? (通過螢火蟲選中)由處理程序返回

XML響應:

<message> 
    <content> 
     <messageId>2</messageId> 
     <from>Barack Obama</from> 
     <fromMail>[email protected]</fromMail> 
     <subject>Yes, we can... get World Peace</subject> 
     <body>Hello, dear citizen. I'm sending you this message to invite you to join us! <a href="http://www.whitehouse.gov">Test link</a> Thank you for your time.</body> 
    </content> 
</message> 

客戶端JavaScript功能以影響用戶界面innerHTML屬性與數據經由AJAX返回:

function GetMessageContentsCallback(args, resp) { 
    //XML Parser 
    try { 
     //Internet Explorer 
     xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
     xmlDoc.async = "false"; 
     xmlDoc.loadXML(resp); 
    } 
    catch (e) { 
     parser = new DOMParser(); 
     xmlDoc = parser.parseFromString(resp, "text/xml"); 
    } 
    var msgReply = xmlDoc.getElementsByTagName('message')[0]; 
    var ajaxRespondeBodyInnerHTML = msgReply.getElementsByTagName(body)[0].firstChild.nodeValue; 
    //this currently only delivers inner text content, without the <a href... bit and subsequent text 
    document.getElementById("bodySpan").innerHTML = ajaxRespondeBodyInnerHTML; 
} 

回答

3

你有正確的想法,但你有一個語法錯誤。我測試,發現問題是這一行預期:

var ajaxRespondeBodyInnerHTML = msgReply.getElementsByTagName("body")[0].firstChild.nodeValue; 

body不加引號。

查看http://jsbin.com/ufubo/edit的工作示例。

編輯:另外,如果你想在你的XML中使用HTML,你需要將你的內容嵌套在CDATA標籤中。我已更新示例以顯示此內容。

編輯:我更新的例子保存它...

+0

謝謝你的回覆。但是,您的解決方案似乎並沒有達到我想要的效果,因爲它不顯示「" element of my XML AJAX response, not only the "" node value. – XpiritO 2009-08-19 16:04:12

+0

See my edit for the solution to your answer. – 2009-08-20 19:17:47

+0

Thanks again for your reply, but your solution doesn't seem to accomplish what I want, as I would like to display ALL node and child nodes content on the placeholder. In the end I would like to have this: placeholder.innerHTML = "Hello, dear citizen. I'm sending you this message to invite you to join us! Test link謝謝您的時間。」 感謝您的幫助;) – XpiritO 2009-08-24 13:37:33

0

您能否使用類似

var ajaxRespondeBodyInnerHTML = msgReply.getElementsByTagName(body)[0].text; 

var ajaxRespondeBodyInnerHTML = msgReply.getElementsByTagName(body)[0].innerHTML; 

那可能有希望抓住一切?

(它已經一段時間,因爲我已經通過AJAX返回的XML工作)

+0

沒有您的建議似乎工作。不過,謝謝你的回覆。 – XpiritO 2009-08-19 16:00:35