Jquery Ajax 异步请求
还是做点笔记,以防下次忘记// ajax 请求获取list function change_third(){ var thirdid=document.getElementById("thirdtree").value;//第3级 var urlStr="url?id="+thirdid; document.getElementById("rightFrame").src=urlStr; $.ajax({ type:"post", url:"<c:url value='/test.do'/>", data:"method=loadSecond&id="+thirdid, success:function(data) { if(data!=0){//返回的list通过json转换因为老出现编码问题,才改用的var tdjson =eval("(" + data + ")");var tdatas = tdjson.classify; var str='';for(var i =0;i<tdatas.length;i++){var count=tdatas.childCount;//就是那个JavaBean、里的指var id=tdatas.id;var title=tdatas.title;} }},error:function(msg){alert("error");}});}}
/** *获取 List **/ public ActionForward loadSecond(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { Long id=Long.parseLong((String)request.getParameter("id")); //前台获取的id List test=clsDao.queryParent(id); response.setCharacterEncoding("GBK");PrintWriter writer = response.getWriter();StringBuffer strXml=null;try {if(parentTree.size()==0){strXml=new StringBuffer("0");}else{ strXml = new StringBuffer("{\"classify\":[");for (int i = 0; i < parentTree.size(); i++) {JavaBean sec=(JavaBean) parentTree.get(i);strXml.append("{\"id\":\"") .append(sec.getNodeId()).append("\",") //节点ID .append("\"childCount\":\"") .append(sec.getChildNum()).append("\",") //子节点数目 .append("\"title\":\"") .append(sec.getTitle()).append("\"}") //节点标题 .append(",");}strXml = new StringBuffer(strXml.substring(0, strXml.length() - 1));strXml.append("]}");}String res = strXml.toString();writer.print(res); //输出信息writer.flush(); //清空缓存,关闭流writer.close();} catch (Exception e) {e.printStackTrace();} return null;}
//如果只要获取一个单独的值,直接打印就OK StringBuffer strBuf=new StringBuffer("TTT");response.setCharacterEncoding("GBK");PrintWriter writer = response.getWriter(); writer.print(strBuf.toString()); writer.flush(); //清空缓存,关闭流writer.close();
页:
[1]