日期:2014-05-16  浏览次数:20786 次

使用jquery ajax做查询时parseerror问题
以前都是用ajax做一些简单的东东,很少用ajax做查询。今日在开发中遇到个比较基础但是却很容易犯的错误,可能会使新手束手无策,于是写此blog分享心得
好了话不多说直接步入正题贴上代码
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<String, Object>
	 */
	@RequestMapping(value = "/statistic/aboutSupply.shtml", method = RequestMethod.GET)
	@ResponseBody
	protected Map<String, Object> 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<String, Object> model = new HashMap<String, Object>();
		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<ApkStatisticRawBean> 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_sta