我今天遇到了一個問題,在用post發(fā)送一個請求到服務(wù)器,然后,返回來了XML的串?dāng)?shù)據(jù),我想通過response.responseXML來取得XML的Document對象,來實現(xiàn)用Javascript對XML文檔的解析,但是,我就只能用response.responseText取得文本數(shù)據(jù),通過response.responseXML取得的對象為null.結(jié)果一直也沒有搞明白。
javascript的代碼如下(用的prototype1.6.js):
function findpoi(){
????//alert("find ");??
????var pname=$F('pnid');
????alert(pname);
????
????var url="poiSearch.do";
????
????var opt = {
?????method:'post',
?????
?????//contentType:'application/xml',
?????//requestHeaders:'text/xml',
?????onComplete:function(transport){
??????alert(transport.status);
??????document.title="ok";
??????if(200==transport.status){
???????
???????//document.title="ok";
???????var cntTxt=transport.responseText;
???????alert(cntTxt);
???????
???????var dobj = transport.responseXML;
???????alert("xml is :? "+dobj);
??????(此處總是取得的是null對象)
???????
???????//var pl = cntTxt.getElementByName("poi");
???????//alert(pl);
???????
???????var ajObj = Ajax.getTransport();
???????alert(ajObj);
????????
????????????
??????}
?????},
?????postBody:"?city=beijing&keyword=kfc"
?????
????};
????
????var ajax=new Ajax.Request(url,opt);
????
???}
我的struts action的代碼如下:
public ActionForward execute(ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response) {
??DynaActionForm poiForm = (DynaActionForm) form;
??String city = (String) poiForm.get("city");
??String keyword = (String) poiForm.get("keyword");
??
??Search bs = new Search();
??bs.baseSearch(city + "/" + keyword);
??PoiDAO poidao = new PoiDAO();
??int count = 10;
??int page = 1;
??ArrayList arry = poidao.by_key_search("北京", "長安商場", count,
????page);
??System.out.println(arry.size());
??
??Document doc= DocumentFactory.getInstance().createDocument();
??doc.setXMLEncoding("UTF-8");
??Element root = doc.addElement("pois");
??
??root.addElement("poi")
??.addAttribute("pid", "1155")
??.addAttribute("longitude", "116.5864")
??.addAttribute("latitude", "39.8195");??
??
??
??ListIterator iter = arry.listIterator();
??while(iter.hasNext()){
??
???Poi p = (Poi)iter.next();
???//float lon = p.getLatitude();
???int id=p.getPoi_id();
???double lon=p.getLongitude();
???double lat = p.getLatitude();
???
???System.out.println("id is :? "+id);
???System.out.println("lon : " + lon);
???
???Element poi=root.addElement("poi")
????.addAttribute("pid", String.valueOf(p.getPoi_id()))
????.addAttribute("longitude", String.valueOf(lon))
????.addAttribute("latitude", String.valueOf(lat));???
??}
??
??try {
???PrintWriter w = response.getWriter();
???
???XMLWriter writer = new XMLWriter(w);
???writer.write(doc);
???
???writer.close();
???w.close();
???
??} catch (IOException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??
??return null;
?}
下午的時候,總是在試圖通過設(shè)置contentType來實現(xiàn),結(jié)果是失敗了。回來的路上,我就一直在想,能取到XML的串,不能取到XML的對象。那么也就是說,返回的數(shù)據(jù)是串,也就是文本類型。突然,我的腦海里閃現(xiàn)了一個問題,是不是返回的contentType沒有設(shè)置成xml.
所以,我就在action里面,添加了
response.setContentType("text/xml");
重新測試,果然成功。
response.responseXML取到了對象。
問題就這樣得到了解決。
問題總結(jié):
如果想要通過AJAX的response.responseXML取得XML Document對象,那么就要在服務(wù)器的相應(yīng)的時候設(shè)置ContextType為text/xml,否則的話,就只能去得到xml的文本串,不能得到DOM對象。
|----------------------------------------------------------------------------------------|
版權(quán)聲明 版權(quán)所有 @zhyiwww
引用請注明來源 http://www.tkk7.com/zhyiwww
|----------------------------------------------------------------------------------------|