日期:2014-05-17  浏览次数:20873 次

怎么能执行到success 里面
<script type="text/javascript">
// alert(nodeId+"/"+parentId+"/"+nodeName+"/"+hrefAddress);
tree = new dTree('tree');//创建一个对象.
$.ajax({ 
url:'tree_hello.action', //调用action方法
type:'post', //数据发送方式 
dataType:'xml', //接受数据格式 
error:function(json){
alert( "not lived!");
},
async: false ,
success: function(xml){
$(xml).find("node").each(function(){ 
var nodeId=$(this).attr("nodeId");  
var parentId=$(this).attr("parentId");  
var hrefAddress=$(this).attr("hrefAddress");  
var nodeName=$(this).text(); 
tree.add(nodeId,parentId,nodeName,hrefAddress,"","","","",false);
});
}
});
document.write(tree);
</script>




//这是action
public void hello()throws Exception{
System.out.println("调用了.........");
HttpServletResponse response = ServletActionContext.getResponse(); 
PrintWriter out = response.getWriter();
  list2= dao.findall();
  System.out.println(list2.size());
  if(list!=null&&list.size()>0){
  out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  out.println("<nodes>");
  for(int i=0;i<list2.size();i++){
  nodes =(Nodes)list2.get(i);
  out.println("<node nodeId='"+nodes.getNodeid()+"' parentId='"+nodes.getParentid()+"' hrefAddress='"+nodes.getHrefaddress()+"'>"+nodes.getNodename()+"</node>");
  }
  out.println("</nodes>");
  }
}


一直返回到error里面。。 就servlet写就能实现。。

------解决方案--------------------


Java code

public void hello()throws Exception{
System.out.println("调用了.........");
HttpServletResponse response = ServletActionContext.getResponse(); 
//增加下面这一句
 response.setHeader("ContentType","text/xml");
PrintWriter out = response.getWriter();
  list2= dao.findall();
  System.out.println(list2.size());
  if(list!=null&&list.size()>0){
  out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  out.println("<nodes>");
  for(int i=0;i<list2.size();i++){
  nodes =(Nodes)list2.get(i);
  out.println("<node nodeId='"+nodes.getNodeid()+"' parentId='"+nodes.getParentid()+"' hrefAddress='"+nodes.getHrefaddress()+"'>"+nodes.getNodename()+"</node>");
  }
  out.println("</nodes>");
  }
}