js对象作为参数传参与后台接收参数

  • Post author:
  • Post category:其他


方式一:序列化js对象

*** js对象的每个属性名需跟后台对象属性名一一对应,否侧不能封装

var params = new Object();

params.status= status;

params.id=centerId;

params.appointmenttime=appointmentTime;

params.problemdescription=problemdescription;

params.appealnature=appealnature;

params.receptionmethod=receptionmethod;

params.reportresponsibility=reportresponsibility;

$.ajax({


type:’POST’,

url:’${ctx}/life/call/updateCenterInfo’,

data:{“params”:JSON.stringify(params)},

success:function(data){


//跳转页面

},error:function(){


alert(“网络异常”);

}

})

后台接收参数:使用字符串接收

JSONObject json = JSONObject.fromObject(params);//将接收的字符串转换为json对象

LifeReceptionCenter bean =(LifeReceptionCenter) JSONObject.toBean(json, LifeReceptionCenter.class);//将json对象转换为LifeReceptionCenter对象

方式二:使用数组封装js对象

var rightData = new Array();/创建数组

var object = new Object();//创建一个对象,封装数据

//封装数据

object[“pkProject”] = ‘${pkProject}’;

object[“pkStage”] = ‘${pkStage}’;

object[“inBatchId”] = ‘${inbatchId}’;

object[“serialNumber”] = serialNumber++;

object[“roomIds”] = rooms;

object[“managerIds”] = managerIds;

object[“managerNames”] = managerNames;

object[“belongBuilding”] = belong_building;

object[“floorNum”] = floorNum;

object[“k”] = jj;

var buildName = $(“#hiddenBuidingName”).val();

object[“buildName”] = buildName;//楼栋名称

rightData.push(object);//将对象封装到数组中去

//encodeURIComponent(JSON.stringify(saveAllRoom));传送参数进行序列化

$.ajax({


type:”POST”,

async: false,

contentType:’application/json’,

url:”${ctx}/life/payHouserAdd/addRoomNum?saveAllRoom=”+encodeURIComponent(JSON.stringify(saveAllRoom))+”&obj=”+obj,

success:function(data){


if(data == “success”){


window.location.href=”${ctx}/life/payHouser/payHouserList”;

}

}

})

后台接收参数:使用字符串接收参数

①//转换为数组

JSONArray json = JSONArray.fromObject(saveAllRoom);

②迭代数组,将其转换为对象

for (int i = 0; i < json.size(); i++) {


//转换对象

JSONObject object = JSONObject.fromObject(json.get(i));

String pkProject = object.getString(“pkProject”);//获取对应属性名的值

}



版权声明:本文为weixin_41802726原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。