---
title: "ajax请求提交list<object>参数"
author: "wander_bird"
date: "2015-01-16T16:06:05Z"
tags: ["Ajax"]
categories: ["Web前端"]
slug: "ajax-list-object"
draft: false
lastmod: "2015-01-23T11:21:03Z"
id: 2176510
url: "https://wander-bird.iteye.com/blog/2176510"
---

\[img]http://dl2.iteye.com/upload/attachment/0105/3266/3e344102-84a4-3449-ab48-ced868021e59.jpg\[/img] \[b]js code\[/b] \[code="java"]$("#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!"); } });\[/code] \[b]server code\[/b] \[b]1.list\[/b] \[code="java"]@RequestMapping(value = "/familySituation/guardian/maintain/{kidId}", produces = "application/json;charset=UTF-8") @ResponseBody public ResultResponse maintainGuardianBatch(@PathVariable("kidId") Long kidId, @RequestBody List 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; }\[/code] \[b]2.array\[/b] \[code="java"]@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 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; }\[/code]