ajax请求提交list参数

[img]http://dl2.iteye.com/upload/attachment/0105/3266/3e344102-84a4-3449-ab48-ced868021e59.jpg[/img]
[b]js code[/b]

$("#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!");
}
});


[b]server code[/b]
[b]1.list[/b]
@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;
}


[b]2.array[/b]
@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;
}

猜你喜欢

转载自blog.csdn.net/ly279481968/article/details/84696613