簡體   English   中英

在AJAX中檢索XML響應

[英]Retrieving XML response in AJAX

我已經編寫了一個Java代碼(Spring控制器)代碼段,以將XML響應發送到AJAX。 不幸的是,我無法獲得響應,XML使用xhr.responseXML ,但可以作為文本使用xhr.responseText 有人會建議實際問題是什么,而不是在客戶端解析文本? 在這里,我附上控制器和AJAX代碼。

Spring controller code:
-------------------------------
String xmlResp = "<cities>";
xmlResp+="<city>";
xmlResp+="<name>" + "Hyderabad" + "</name>";
xmlResp+="<population>" + "3000000" + "</population>";
xmlResp+="</city>";
xmlResp+="<city>";
xmlResp+="<name>" + "Bangalore" + "</name>";
xmlResp+="<population>" + "4500000" + "</population>";
xmlResp+="</city>";
xmlResp+="</cities>";

response.setContentType("text/xml");
response.getWriter().write(xmlResp);


AJAX code:
-----------------------------

reading as XML:

var xml=xhr.responseXML;
        alert(xml);

reading as Text
var text=xhr.responseText;
        alert(text);

為了方便起見,XML結構:

<cities>
    <city>
        <name>Hyderabad</name>
        <population>3000000</population>
    </city>
    <city>
        <name>Bangalore</name>
        <population>4500000</population>
    </city>
</cities>

您的XML格式不正確:

String xmlResp = "'<'cities>";

會給:

'<'cities>
  • 使用驗證器或棉絨檢查您的XML
  • 不要通過將字符串混在一起來構建XML文檔,而應使用XML庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM