ITeye博客

ajax请求提交list<object>参数

wander_bird · 2015-01-16 16:06 · js&ajax · Web前端 · Markdown

js code

$("#saveFamilySituationBtn").click(function(){
		var isSuccess = false;

		//创建guardian数组
		var guardianList = [];
		//遍历guardian div,找出非完全的guardian div
		$("div[name='guardian']").each(function(index){
			//设立非空guardian div flag
			var flag = false;
			$(this).find("input").each(function(){
				if($(this).attr("type") == "checkbox"){
					if($(this).attr("checked")){
						flag = true;
						return false;
					}
				}else{
					if($(this).val().trim() != ""){
						flag = true;
						return false;
					}
				}
			});

			if(flag){
				//创建guardian对象
				var guardian = {};
				index += 1;
				
				guardian.name = $("#guardianName"+index).val().trim();
				guardian.relationship = $("#guardianRelationship"+index).val().trim();
				if($("#liveWith"+index).attr("checked")){
					guardian.liveWith = true;
				}else{
					guardian.liveWith = false;
				}

				guardian.id = $("#guardianId"+index).val().trim();
				guardian.address = $("#address"+index).val().trim();
				guardian.apartment = $("#apartment"+index).val().trim();
				guardian.postalCode = $("#postCode"+index).val().trim();
				guardian.homePhone = $("#homePhone"+index).val().trim();
				guardian.mobilePhone = $("#mobilePhone"+index).val().trim();
				guardian.workPhone = $("#workPhone"+index).val().trim();
				guardian.email = $("#email"+index).val().trim();
				if(guardian.name == null || guardian.name==""){
					common.showMessage("Please type guardian name first!");
					return false;
				}
				if(guardian.mobilePhone == null || guardian.mobilePhone==""){
					common.showMessage("Please type mobile phone first!");
					return false;
				}
				guardianList.push(guardian);
			}
		});
		//若guardian 数组有值,则提交ajax请求
		if(guardianList.length != 0){
			$.ajax({ 
		        type: "post",
		        dataType: "json", 
		        traditional:true,
		        async : false,
		        data: JSON.stringify(guardianList),
		        url: "${maintainGuardianURL}"+$("#id").val(),
		        headers : {
		            'Accept' : 'application/json',
		            'Content-Type' : 'application/json'
		        },
		        success: function (data, textStatus) {
		            if (!data.result) {
		            	isSuccess = false;
		            }else{
		            	isSuccess = true;
			        }
		        },
		        error: function (e) {
		        	isSuccess = false;
		        }
		    });
		}
		
		var siblingList = [];
		$("div[name='sibling']").each(function(index){
			var flag = false;
			$(this).find("input").each(function(){
				if($(this).attr("type") != "checkbox"){
					if($(this).val().trim() != ""){
						flag = true;
						return false;
					}
				}
			});

			if(flag){
				var sibling = {};
				index += 1;

				sibling.kidId = $("#kidId"+index).val().trim();
				sibling.kidName = $("#siblingName"+index).val().trim();
				sibling.relationship = $("#relationship"+index).val().trim();
				if($("#enrolled"+index).attr("checked")){
					sibling.enrolled = true;
				}else{
					sibling.enrolled = false;
				}

				siblingList.push(sibling);
			}
		});

		if(siblingList.length != 0){
			$.ajax({ 
		        type: "post",
		        dataType: "json", 
		        traditional:true,
		        async : false,
		        data: JSON.stringify(siblingList),
		        url: "${maintainSiblingURL}"+$("#id").val(),
		        headers : {
		            'Accept' : 'application/json',
		            'Content-Type' : 'application/json'
		        },
		        success: function (data, textStatus) {
		            if (!data.result) {
		            	isSuccess = false;
		            }else{
		            	isSuccess = true;
			        }
		        },
		        error: function (e) {
		        	isSuccess = false;
		        }
		    });
		}

		if(isSuccess){
			common.showMessage("save success!");
		}else{
			common.showMessage("save failed, please try again!");
		}
	});

server code 1.list

@RequestMapping(value = "/familySituation/guardian/maintain/{kidId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultResponse maintainGuardianBatch(@PathVariable("kidId") Long kidId,
			@RequestBody List<AppGuardianVO> guardianList) {

		ResultResponse result = new ResultResponse();
		if (null != guardianList) {
			try {
				kidEditSerivce.maintainGuardianBatch(guardianList, kidId);
				result.setResult(true);

			} catch (Exception e) {
				result.setResult(false);
				result.setMessage("修改出异常,请重试!");
			}
		}

		return result;
	}

2.array

@RequestMapping(value = "/familySituation/sibling/maintain/{kidId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultResponse maintainSiblingBatch(@PathVariable("kidId") Long kidId,
			@RequestBody AppSiblingVO[] siblingArray) {

		ResultResponse result = new ResultResponse();
		List<AppSiblingVO> siblingVOList = null;
		if (null != siblingArray) {
			siblingVOList = Arrays.asList(siblingArray);
			try {
				kidEditSerivce.maintainSiblingBatch(siblingVOList, kidId);
				result.setResult(true);

			} catch (Exception e) {
				result.setResult(false);
				result.setMessage("修改出异常,请重试!");
			}
		}

		return result;
	}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。