一、 ajax返回json格式 1、客户端js function Co_Plqz(formObj,sSingleCmd, sCtrlId,nPos) { slid=slidstr[v]; //slid值 $.ajax({ //使用ajax异步调用后台传递参与并获取返回值 type:"post", data: {slid:slid},//传递slid值 url:"plqzAction_getInfoBySlid.do", dataType:"json",//返回json格式 async :false, success:function(data){ for(var i=0;i<data.length;i++){ var tempslid=data[i].slid; slid=tempslid.trim();//"M20140001308" formid=data[i].formid; } } }) package com.kanq.jsgt.action;
import java.io.PrintWriter; import java.util.List; import net.sf.json.JSONArray; import com.kanq.jsgt.dao.PlqzDao;
public class PlqzAction extends BaseAction{
PlqzDao plqzDao = new PlqzDao(); /* 根据受理编号,获取信息 * @param slid * @return */ public String getInfoBySlid(){User sys = (User) this.servletRequest.getSession().getAttribute( "mhUser");String getslid=this.servletRequest.getParameter("slid"); try{ PrintWriter out = this.servletResponse.getWriter(); List list =plqzDao.getInfoBySlid(getslid); System.out.println("批量签章 list size="+list.size()); JSONArray obj = JSONArray.fromObject(list); out.print(obj); }catch (Exception e){ e.printStackTrace(); } return null; } }//注:PrinWriter要加格式输出设置:this.servletResponse.setContentType("text/html;charset=UTF-8"); 3、实现类 二、 ajax返回text格式 1、客户端js var j = ScanStr(a, f, 0, 2, 0, 1);//获取选中的地块编号 //根据选中的地块编号,返回地块中每个点的xy坐标,重新组合J的数据 add by wgy 20150109 $.ajax({ type:"post", data:{j:j}, url:"../importAction_getData.do", dataType:"text",//看后台返回的是json还是文本格式 async :false, success:function(data){ j=data; var k = j.split(','); if (k.length > 3) { var l = k.length / 3; j = ''; for (var i = 0; i < l; i++) { j += "<dr"; j += " x=\"" + k[i * 3] + "\""; j += " y=\"" + k[i * 3 + 1] + "\""; j += " d=\"" + k[i * 3 + 2] + "\""; j += "/>" } } h.drawMap(j) k = null; j = null; j = null; h = null } }) 2、控制类/** * 根据地块id获取反馈的地块点x,y坐标 * @param dkid * @return */ public void getData(){//ajax异步调用。 public void getData(String j) 这个写法是不正确的,出现的问题1、不走这个action 2、提示java.lang.NoSuchMethodException: com.kanq.jsgt.action.OperAction.getData() String getdkid=this.servletRequest.getParameter("j"); System.out.println("动态获取的值J="+getdkid); HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); try { PrintWriter out= response.getWriter(); String str=this.operDao.getStr(getdkid); System.out.println("输出字符串str="+str); out.println(str); } catch (IOException e) { e.printStackTrace(); } } 3、实现类/** * 根据地块id获取反馈的地块点x,y坐标 * @param dkid * @return */ public String getStr(String dkid){ String strs=""; String dkdsql="select xzb,yzb,czf from ya_kcdjcgb_b where kcdjcgbid in("+dkid+")";// try { this.pre = this.conn.prepareStatement(dkdsql); System.out.println("根据地块,获取地块点的SQL="+dkdsql); this.rs=this.pre.executeQuery(); int n=0;//判断循环个数 int dkcount=0;//判断地块个数 int j=0; int g=0; while(rs.next()){ n++; if(g>0){ j++; } System.out.println(n); System.out.println(g); System.out.println(j); if(rs.getString("czf")== null){ if(dkcount>=1 && j>=1){//有地块结束并且还有新的地块点开始 str="G,"+rs.getString("xzb")+","+rs.getString("yzb")+",";//用G隔开不同地块 j=0;//对新地块的开始统计为0 g=-1; }else{ str=rs.getString("xzb")+","+rs.getString("yzb")+","; } }else{ str=rs.getString("xzb")+","+rs.getString("yzb")+","+rs.getString("czf");//地块结束 dkcount++; j=0; g=1; } System.out.println("str="+str); if(n>1){ strs+=","+str; //实现字符串的追加 }else{ strs=str; } System.out.println("strs="+strs); } } catch (SQLException e) { e.printStackTrace(); } return strs; } |
jquery ajax简单示例
最新推荐文章于 2024-01-02 21:16:13 发布