`
hekuilove
  • 浏览: 156350 次
  • 性别: Icon_minigender_1
  • 来自: 魔都
社区版块
存档分类
最新评论

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_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> apkList;
...
}
问题就出在这里了...我的ApkBean里面有JarBean JarBean中有List<ApkBean> 
在将List转为JSON的时候会解析到ApkBean然后里面有JarBean又开始解析JarBean然后JarBean里面又有List<APkBean> 然后又开始解析ApkBean了....
死循环了...!!!
说到这里 相比不明白的差不多也明白了吧...如果你的POJO里面有太多的关联最好不要直接返回..而是针对要返回的需求再创建一个POJO内容自己填充 我这里就是这样解决的
  • 描述: 弹出错误
  • 大小: 4.3 KB
  • 描述: 返回的JSON数据
  • 大小: 202.7 KB
分享到:
评论

相关推荐

    空格或者空白字符导致$.ajax()报parseerror错误小结

    您可能感兴趣的文章:JQuery ajax中error返回错误及一直返回error的解答jquery中ajax使用error调试错误的方法ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信息Jquery ajax执行顺序 返回自

    jQuery 1.4.1 中文参考

    11.2.10 jQuery.parseJSON(json) 189 11.3 函数操作 190 11.3.1 jQuery.noop 190 11.3.2 jQuery.proxy(function, scope) 190 11.4 测试操作 191 11.4.1 jQuery.contains(container, contained) 191 11.4.2 jQuery....

    jQuery 1.6.3正式版发布

    #9255:修复webkit内核浏览器中jQuery.parseXML不能处理异常的问题。 #9854:Pass statusText through instead of “normalizing” it #9887:修复jQuery.ajaxSetup可能导致内存浪费的问题。 #9970:Typo in ajax.js...

    jQuery 1.5 API 中文版

    objjQuery.parseJSON( str ) Data functions $.clearQueue( [name] ) $.dequeue( [name] ), jQuery.dequeue( [name] ) objjQuery.data( element, key ), jQuery.data( ) obj.data( ), .data( key ) $.data( key, val...

    jquery1.11.0手册

    ajaxError(callback) ajaxSend(callback) ajaxStart(callback) ajaxStop(callback) ajaxSuccess(callback) 其它 $.ajaxPrefilter([type],fn) $.ajaxSetup([options]) serialize() serializearray() 工具 ...

    AJAX and PHP.pdf

    Chapter 8: AJAX Chat with jQuery teaches how to use AJAX to easily implement an online chat solution. This will also be your opportunity to use one of the most important JavaScript frameworks around...

    jQuery1.4 API

    [queueName]) 设置 jQuery.fx.off Ajax Ajax 请求 $.ajax([options]) load(url, [data], [callback]) $.get(url, [data], [fn], [type]) $.getJSON(url, [data], [fn]) $.getScript(url, [callback]) $.post(url, ...

    ajaxfileupload.js

    data = jQuery.parseJSON(data); success(data); }, error: function (data, status, e) { error(e); }, complete: function (XMLHttpRequest, textStatus) { //hide loading... }, change: change //需要...

Global site tag (gtag.js) - Google Analytics