第一种情况:::: 传递 json对象 !!
(?﹏?)
原博:https://blog.csdn.net/qq_42131246/article/details/83105221
贴两个博文:
https://blog.csdn.net/ssssny/article/details/83986684
https://blog.csdn.net/qq_37279783/article/details/80910008
前端注意:
- contentType: ‘application/json;charset=utf-8’(设置发送数据类型为json)
- data: JSON.stringify(对象或者数组)(序列化json数据)
$.ajax({
url : "/post/gettext",
type : "post",
// traditional:true, //防止深度序列化
data :
JSON.stringify(list),
dataType:"text",
contentType : "application/json",
success : function(data) {
alert("..");
}
});
list:
JSON.stringify(list):
后端注意: SpringMVC接收复杂参数必须要在参数前加 @RequestBody
@RequestMapping(value = "/gettext",method = RequestMethod.POST)
public void getText(@RequestBody List<ReceiveModel> list) {
for (ReceiveModel receiveModel : list) {
System.out.println(receiveModel.toString());
}
}
添加一次使用类型 :)
前 :
function jsonvalue() {
var model={};
model.list=[];
model.idInteger = 5;
model.list.push({"type":5,"value":"eng"},{"type":6,"value":"ang"});
model.name="nihao";
model.sex=true;
console.log(JSON.stringify(model));
$.ajax({
url : "/post/gettext1",
type : "post",
traditional:true,
data :
JSON.stringify(model),
dataType:"text",
contentType : "application/json",
success : function(data) {
console.log("返回: "+data);
}
});
}
后 :
@PostMapping("/gettext1")
public String GetText1(@RequestBody User model) {
return model.toString();
}
模型:
public class User {
private Integer idInteger;
private List<ChoiceModel> list;
private String name;
private boolean sex;
public Integer getIdInteger() {
return idInteger;
}
public List<ChoiceModel> getList() {
return list;
}
public String getName() {
return name;
}
public boolean isSex() {
return sex;
}
@Override
public String toString() {
return "User [idInteger=" + idInteger + ", list=" + list + ", name=" + name + ", sex=" + sex + "]";
}
}
第二种情况:::: 传递 json串 !!
(以后写…)