1:页面使用js对象
注意:如果涉及到对象数组 那么就不能使用js对象来传输 (包括json形式的js对象 其实 json对象和js对象区别在于key是否可 以用“”包起来)
// json形式的js对象
var user ={
"name":"tom",
"password":"123456",
"address":[{"province":"wuhan","city":"wuan"},{"province":"enshi","city":"enshi"}],
"info":[{"age":22,"habbit":"pp"},{"age":26,"habbit":"lq"}]
};
解决办法:两种选择
a:继续使用js对象 只不过不是对象数组
var index = 0;
var extras={};
extras['supplierList['+index+'].seq'] = "1";
extras['supplierList['+index+'].companyId'] = "U34663";
extras['supplierList['+index+'].supplierCode'] ="U34663";
extras['supplierList['+index+'].supplierName'] = "中北";
extras['supplierList['+index+'].linkmanName'] = "tom";
extras['supplierList['+index+'].answerUsername'] = "jack";//适配报名表联系人
extras['supplierList['+index+'].linkmanTelphone'] = "110";
extras['supplierList['+index+'].isBindWechat'] = "erfrerf";
b:使用下面的第二种将json形式的js对象转为json字符串
// json形式的js对象
var user ={
"name":"tom",
"password":"123456",
"address":[{"province":"wuhan","city":"wuan"},{"province":"enshi","city":"enshi"}],
"info":[{"age":22,"habbit":"pp"},{"age":26,"habbit":"lq"}]
};
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/sendJson1.action",
contentType:"application/json;charset=utf-8",
data:JSON.stringify(user),
dataType:'json',
success:function(data) {
console.log(data);
}
});
2:使用json字符串传入后台,Controller中使用@requestBody注解将请求体中的json字符串转为javaBean