好了话不多说直接步入正题贴上代码
jquery请求代码:
$("#show_supply_table").click(function() { var datas={time:time,event:event,supply:supply,productName:productName}; var paths=path+"/statistic/aboutSupply.shtml"; $.ajax({ type:"get", url:paths, dataType:"json", data:datas, success:function(data){ alert("对鸟!!"); },error:function(a,b){ alert(b); } }); });
但是重点不在jquery里,再看java代码 注:我这里是使用的spring的Controller,这里重点不在框架,而是在返回的结果,使用任何框架都是一样的.因为你返回的东西都是一样
/** * 处理ajax请求,返回查询结果 * * @author Quinn He * @param time 时间范围 * @param event 事件 * @param supply 供应商 * @param productName 产品名 * @dateTime 2012-3-2 下午2:19:01 * @param request * @param response * @return java.util.Map*/ @RequestMapping(value = "/statistic/aboutSupply.shtml", method = RequestMethod.GET) @ResponseBody protected Map aboutSupply(@RequestParam(value = "time") String time, @RequestParam(value = "event") String event, @RequestParam(value = "supply") String supply, @RequestParam(value = "productName") String productName, final HttpServletRequest request, final HttpServletResponse response) { Map model = new HashMap (); time = StringUtils.isEmpty(time) ? null : time; event = StringUtils.isEmpty(event) ? null : event; supply = StringUtils.isEmpty(supply) ? null : supply; productName = StringUtils.isEmpty(productName) ? null : productName; String[] params = { time, event, supply, productName }; List list = this.querySupplyDetail(params, null); model.put("aboutSupplys", list); return model; }
通过这段代码可以看到本人返回了一个List对象,看似毫无问题的代码,可是它偏偏就出了问题...jquery里始终是执行的
error:function(a,b){ alert(b); }
于是我疑惑的打开了firefox,通过firefox我发现请求响应200了
图片上的内容就是返回的是将LIST转换为JSON集合后的结果,先别管其它,至少知道了数据已经返回回来了.返回回来了为什么还error了呢?并且还提示parseerror错误?
问题就在最后一部转换的时候,LIST转换为JSON的时候
看看ApkStatisticRawBean里面究竟是什么东东
import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import me.gall.business.model.mybatis.bean.ApkStatisticRaw; import me.gall.business.model.mybatis.bean.SupplyInfo; import me.gall.business.service.init.SystemServiceImpl; /** * @author Quinn He * @dateTime 2012-2-9 下午4:35:18 */ public class ApkStatisticRawBean { /** * @author Quinn He * @dateTime 2012-2-29 下午6:06:24 * @param bean */ public ApkStatisticRawBean(ApkStatisticRaw bean) { this.id = bean.getId(); this.uuid = bean.getUuid(); if (bean.getApkId() != null) { this.apk = SystemServiceImpl.getInstance().getBaseApkMap().get(bean.getApkId()); } if (bean.getChannelId() != null) { this.channel = SystemServiceImpl.getInstance().getChannels().get(bean.getChannelId()); } if (bean.getSupplyId() != null) { this.supply = SystemServiceImpl.getInstance().getSupplys().get(bean.getSupplyId()).getSupply(); } this.content = bean.getContent(); if (bean.getCreateTime() != null) { this.createTime = new Date(bean.getCreateTime()); } this.creator = bean.getCreator(); if (bean.getEventId() != null) { this.event = SystemServiceImpl.getInstance().getApkStatisticCategoryEventMap().get(bean.getEventId()); } this.numbers = bean.getNumbers(); this.other = bean.getOther(); this.productName = bean.getProductName(); this.status = bean.getStatus(); if (bean.getTime() != null) { this.time = new Date(bean.getTime()); } } /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.id * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private Integer id; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.uuid * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private String uuid; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.apk_id * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private ApkBean apk; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.event_id * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private ApkStatisticCategoryEventBean event; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.supply_id * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private SupplyInfo supply; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.channel_id * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private ChannelBean channel; /** * This field was generated by MyBatis Generator. This field corresponds to the database column * apk_statistic_raw.product_name * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private String productName; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.content * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private String content; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.time * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private Date time; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.numbers * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private Integer numbers; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.status * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private Integer status; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.creator * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private String creator; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.create_time * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private Date createTime; /** * This field was generated by MyBatis Generator. This field corresponds to the database column apk_statistic_raw.other * * @mbggenerated Thu Feb 09 17:04:23 CST 2012 */ private String other; get..... set.....
原来是我在此对象里面封装了对象
private ApkBean apk;
/** * @author Quinn He * @dateTime 2011-11-15 下午2:20:27 */ public class ApkBean { ... private JarBean jar; ... }
public class JarBean { ... private List问题就出在这里了...我的ApkBean里面有JarBean JarBean中有ListapkList; ... }
在将List转为JSON的时候会解析到ApkBean然后里面有JarBean又开始解析JarBean然后JarBean里面又有List
死循环了...!!!
说到这里 相比不明白的差不多也明白了吧...如果你的POJO里面有太多的关联最好不要直接返回..而是针对要返回的需求再创建一个POJO内容自己填充 我这里就是这样解决的