EXTJS AJAX 從後台取 XML 資料 在頁面上展示
頁面JS
function getAllAreaInfos(){var areaName = "";Ext.Ajax.request({ method: 'POST', url: 'allAreaInfos', params: {"areaName":areaName}, waitMsg: '資料更新中,請稍後...', success: function(response,options){ var xmlDoc = response.responseXML; if(xmlDoc!=null) { var ais = xmlDoc.getElementsByTagName("Areainfo"); if(ais!=null&&ais.length>0) { for(var i=0;i<ais.length;i++)// DB { var asId = (ais[i].getElementsByTagName("id")[0].childNodes[0].nodeValue); // 第i個areaInfos的id var asName = (ais[i].getElementsByTagName("name")[0].childNodes[0].nodeValue); // 第i個areaInfos的名字 var allCapacity = (ais[i].getElementsByTagName("allCapacity")[0].childNodes[0].nodeValue); // 第i個areaInfos的總裝機容量 //document.getElementById(asName).innerHTML=""; document.getElementById(asName).innerHTML=allCapacity;//得到的資料在頁面上展示 } } } }, failure: function(response,options){ Ext.Msg.alert("資訊提示", "擷取資料庫資料出錯,請稍後再試!"); }, autoAbort:false});}
action
public String getAllAreaInfos() {try {List<Areainfo> list = null;if(areaName!=null&&!areaName.equalsIgnoreCase("null")&&areaName.trim().length()>0){list = mapInfos.findAllAreaInfos(this.subStringAreaName(areaName));}elselist = mapInfos.findAllAreaInfos(null);response.setContentType("text/xml;charset=utf-8");response.setCharacterEncoding("utf-8");PrintWriter pw = null;pw = response.getWriter();StringBuffer sb = new StringBuffer();sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");sb.append("<Items>");if(list==null){sb.append("<Areainfo>");sb.append("<id>").append("no").append("</id>");sb.append("</Areainfo>");}else{for (int i = 0; i < list.size(); i++) {Areainfo v = list.get(i);sb.append("<Areainfo>");sb.append("<id>").append(v.getId()).append("</id>");sb.append("<name>").append(v.getName()).append("</name>");sb.append("<allCapacity>").append(v.getAllcapacity()).append("</allCapacity>");sb.append("</Areainfo>");}}sb.append("</Items>");String xmlDBTree = sb.toString();pw.print(xmlDBTree);pw.flush();pw.close();} catch (Exception e) {e.printStackTrace();}return null;}