ajax 施用jquery处理返回的xml
ajax 使用jquery处理返回的xml
public String buildTree() throws Exception{ List<TwiooDictionary> list = dictionaryManager.findTwiooDictionaryList(); StringBuffer buf = new StringBuffer(); buf.append("<root>"); for(TwiooDictionary td : list){ buf.append("<item>"); buf.append("<id>" + td.getId() + "</id>"); buf.append("<name>" + td.getName() + "</name>"); buf.append("<pid>" + td.getParentId() + "</pid>"); buf.append("</item>"); } buf.append("</root>"); callbackClient(this.getResponse(),buf.toString()); return null; } private void callbackClient(HttpServletResponse response,String js)throws IOException{ try{ response.setContentType("text/xml;charset=UTF-8"); response.setHeader("Cache-Control", "on-cache"); java.io.PrintWriter write = response.getWriter(); write.write(js); write.close(); }catch(Exception e){ e.printStackTrace(); } }
上面得注意
response.setContentType("text/xml;charset=UTF-8"); 与 response.setContentType("text/html;charset=UTF-8"); 是不一样的,如果写成html下面 $(data).find('item').each(function() 可能 取不了值
<script type="text/javascript"> $(document).ready(function(){ $.ajax({ type: "GET", url: "buildTreeDictionary.action", success: function(data){ d = new dTree('d'); d.config.target = "mainFrame2"; $(data).find('item').each(function(){ var name = $(this).find('name').text(); var id = $(this).find('id').text(); var pid = $(this).find('pid').text(); d.add(id,pid,name,'editNodeDictionary.action?nodeId='+id); }); //alert("============="+d); // alert(d.toString()); $("#dtree2").html(d.toString()); } }); }); </script>