ajax与后台之间传递多个对象方法

ajax传递多组对象给后台

如果是需要通过ajax传递数组给后台,可以使用JSON.stringify()函数将JS数组转为json字符串,然后后台通过@RequestBody注解修饰,将前台传来的json字符串转为对应的参数类型。
前台

$.ajax({
		type: "post",//注意不能用get
		dataType: 'json',		//指定参数类型
		url: "customerInfoCT/limitQuery",
		contentType: 'application/json;charset=utf-8',		//这个必须是这个格式
		data: JSON.stringify(screenInfo),//前台将要传递的数组封装成json格式
		traditional: true,
		success: function (ret) {
		}
	});

后台

@RequestMapping("completeQuery")
	@ResponseBody
	public String customerSort(@RequestBody List<String> screenInfo){
		return null;
	}

而如果在传递数组的同时还需要传递其它参数,可以在url尾部追加,因为在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,其中json字符串需要使用@RequestBody注解获取@RequestParam()可以有多个,而@RequestBody最多只能有一个,因为传递类型指定为了json,如果再到data中添加参数,它们也都会以json格式传递,后台接到的往往就为null(详情可以看这篇博客),所以可以尝试通过url传递。
前台

$.ajax({
		type: "post",//注意不能用get
		dataType: 'json',
		url: tableName+"CT/completeQuery?pageIdx="+pageIdx+"&pageDataCount="+pageDataCount,
		contentType: 'application/json;charset=utf-8',//这个必须是这个格式
		data: JSON.stringify(screenInfo),//前台要封装成json格式
		traditional: true,
		success: function (ret) {
		}
	});

后台

@RequestMapping("completeQuery")
	@ResponseBody
	public String customerSort(@RequestBody List<String> screenInfo,@RequestParam Integer pageIdx,@RequestParam Integer pageDataCount){
		return null;
}

后台传递多组对象给ajax

后台有时也需要传递多个不同类型的对象给前台,例如表模型数组、当前访问页数、每页显示行数等。你可以选择通过创建一个类,将这些对象封装到一起,然后用需要传递的对象创建该类实例,把该类传递给前台回调函数,但也可以选择一种更简单的方式,使用非泛型的HashMap存储要传递的所有对象,然后将该hashMap返回给回调即可。
后台

@RequestMapping("completeQuery")
	@ResponseBody
	public HashMap customerSort(@RequestBody List<String> screenInfo,@RequestParam Integer pageIdx,@RequestParam Integer pageDataCount){
		//业务代码可以忽略
		QueryInfoByCustomerInfo qc = new QueryInfoByCustomerInfo(screenInfo,pageIdx*pageDataCount,pageDataCount);
		int count = cService.findScreenCount(qc);
		pageIdx = count>pageIdx*pageDataCount?pageIdx:(count-1)/pageDataCount;
		qc.setLimitBegin(pageIdx*pageDataCount);
		List<CustomerInfo> customerInfos = cService.completeQuery(qc);
		//存储
		HashMap hash = new HashMap();
		hash.put("customerInfos", customerInfos);
		hash.put("pageIdx",pageIdx);
		hash.put("dataCount", count);
		
		return hash;
	}

前台

$.ajax({
		type: "post",//注意不能用get
		dataType: 'json',
		url: tableName+"CT/completeQuery?pageIdx="+pageIdx+"&pageDataCount="+pageDataCount,
		contentType: 'application/json;charset=utf-8',
		data: JSON.stringify(screenInfo),//前台要封装成json格式
		traditional: true,
		success: function (hash) {
			console.log(hash);
			var customerInfos = hash.customerInfos;
			dataCount = hash.dataCount;
			pageIdx = hash.pageIdx;
			refreshDataPageLinks();
			var datatable = document.getElementsByClassName("datatable")[0];
			$('.datatable .row').remove();
			for(var i = 0;i < customerInfos.length;i++){
				var $newRow = $dataRowTemp.clone(true);
				var $vals = $newRow.children('.val');
				$vals.eq(0).html(customerInfos[i].id);
				$vals.eq(1).html(customerInfos[i].salesman);
				$vals.eq(2).html(customerInfos[i].customerId);
				$vals.eq(3).html(customerInfos[i].proportionOfOrders);
				$vals.eq(4).html(customerInfos[i].cumulativeProportion);
				$vals.eq(5).html(customerInfos[i].averageMonthlyOrder);
				$vals.eq(6).html(customerInfos[i].totalOrders);
				$vals.eq(7).html(customerInfos[i].orderNumber);
				$vals.eq(8).html(customerInfos[i].orderQuantity);
				$vals.eq(9).html(customerInfos[i].customerLevel);
				$vals.eq(10).html(customerInfos[i].leadTime);
				$vals.eq(11).html(customerInfos[i].remark);
				$('.datatable').append($newRow);
			}
		}
	});
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值